Countdown 倒計時
顯示距離某個目標時間還剩多久,到時間後觸發事件。用於活動截止、驗證碼重發、限時操作等場景。
引入
import { HuiCountdown } from '@nscode/handy-ui'
基礎用法
value 傳目標時間的毫秒時間戳。元件按“目標時間 - 目前時間”顯示,所以頁面切到後臺再回來、電腦休眠後恢復,顯示的時間都是準的。
<script setup lang="ts">
import { HuiCountdown } from '@nscode/handy-ui'
// 1 小時後
const targetTime = Date.now() + 60 * 60 * 1000
</script>
<template>
<HuiCountdown :value="targetTime" title="距離活動結束" />
</template>
目標時間要先算好存起來。不要在模板裡寫 :value="Date.now() + 10000",這樣父元件每次重新呈現都會得到一個新的目標時間,倒計時會被不斷重置。
目標時間傳 Date
value 也可以直接傳 Date 物件。
<script setup lang="ts">
import { HuiCountdown } from '@nscode/handy-ui'
// 今天 24:00
const endOfToday = new Date()
endOfToday.setHours(24, 0, 0, 0)
</script>
<template>
<HuiCountdown :value="endOfToday" title="距離今天結束" />
</template>
格式
format 控制顯示格式,預設 HH:mm:ss。可用的單位:
| 單位 | 含義 | 說明 |
|---|---|---|
D | 天 | D 不補零,DD 補到 2 位 |
H | 小時 | H 不補零,HH 補到 2 位 |
m | 分鐘 | m 不補零,mm 補到 2 位 |
s | 秒 | s 不補零,ss 補到 2 位 |
S | 毫秒 | S 是百毫秒(1 位),SS 是十毫秒(2 位),SSS 是毫秒(3 位) |
格式裡的其他字元原樣輸出。要輸出的文字裡含有上面這些字母時,用方括號括起來,例如 D [days] HH:mm:ss。
<script setup lang="ts">
import { HuiCountdown, HuiSpace } from '@nscode/handy-ui'
// 1 天 2 小時 3 分鐘後
const targetTime = Date.now() + ((24 + 2) * 60 + 3) * 60 * 1000
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiCountdown :value="targetTime" format="DD 天 HH:mm:ss" />
<HuiCountdown :value="targetTime" format="D 天 H 小时 m 分 s 秒" />
<HuiCountdown :value="targetTime" format="HH时mm分ss秒" />
<HuiCountdown :value="targetTime" format="D [days] HH:mm:ss" />
</HuiSpace>
</template>
缺少大單位時折算
格式裡沒有寫的大單位會折算進下一級單位。例如還剩 1 天 2 小時 3 分鐘:
<script setup lang="ts">
import { HuiCountdown, HuiSpace } from '@nscode/handy-ui'
// 1 天 2 小時 3 分鐘後
const targetTime = Date.now() + ((24 + 2) * 60 + 3) * 60 * 1000
</script>
<template>
<HuiSpace direction="vertical" align="start">
<!-- 1 天 02:03:00 -->
<HuiCountdown :value="targetTime" format="D 天 HH:mm:ss" />
<!-- 26:03:00 -->
<HuiCountdown :value="targetTime" format="HH:mm:ss" />
<!-- 1563:00 -->
<HuiCountdown :value="targetTime" format="mm:ss" />
<!-- 93780 秒 -->
<HuiCountdown :value="targetTime" format="s" suffix="秒" />
</HuiSpace>
</template>
毫秒
格式裡帶 S 就會顯示毫秒:S 是百毫秒(1 位),SS 是十毫秒(2 位),SSS 是毫秒(3 位)。
<script setup lang="ts">
import { HuiCountdown, HuiSpace } from '@nscode/handy-ui'
// 10 分鐘後
const targetTime = Date.now() + 10 * 60 * 1000
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiCountdown :value="targetTime" format="mm:ss.SSS" title="SSS" />
<HuiCountdown :value="targetTime" format="mm:ss.SS" title="SS" />
<HuiCountdown :value="targetTime" format="mm:ss.S" title="S" />
</HuiSpace>
</template>
標題、前綴和後綴
title、prefix、suffix 傳文字;需要圖示或其他內容時用同名插槽,插槽優先於屬性。
<script setup lang="ts">
import { HuiCountdown, HuiIcon, HuiSpace } from '@nscode/handy-ui'
const targetTime = Date.now() + 3 * 60 * 1000
</script>
<template>
<HuiSpace wrap size="large">
<HuiCountdown :value="targetTime" title="限時搶購" prefix="僅剩" suffix="秒" format="s" />
<HuiCountdown :value="targetTime">
<template #title>秒殺倒計時</template>
<template #prefix>
<HuiIcon name="clock" />
</template>
<template #suffix>後結束</template>
</HuiCountdown>
<HuiCountdown :value="targetTime" format="mm:ss">
<template #title>
<span style="color: var(--hui-color-danger)">距離關閉報名</span>
</template>
</HuiCountdown>
</HuiSpace>
</template>
自訂數值樣式
valueStyle 是加在數值上的行內樣式。
<script setup lang="ts">
import { HuiCountdown, HuiSpace } from '@nscode/handy-ui'
// 1 小時後
const targetTime = Date.now() + 60 * 60 * 1000
</script>
<template>
<HuiSpace wrap size="large" align="end">
<HuiCountdown
:value="targetTime"
:value-style="{ color: 'var(--hui-color-primary)', fontSize: '32px' }"
/>
<HuiCountdown
:value="targetTime"
title="危險色、等寬數字"
:value-style="{ color: 'var(--hui-color-danger)', fontFamily: 'var(--hui-font-family-mono)' }"
/>
<HuiCountdown
:value="targetTime"
title="小號加粗"
:value-style="{ fontSize: '16px', fontWeight: 600 }"
/>
</HuiSpace>
</template>
事件和重新計時
倒計時到 0 時觸發 finish。value 變化後會按新的目標時間重新計時,結束之後改成新的時間也可以再次觸發 finish。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCountdown, HuiButton, HuiSpace } from '@nscode/handy-ui'
const targetTime = ref(Date.now() + 5000)
const finished = ref(false)
const restart = () => {
finished.value = false
targetTime.value = Date.now() + 5000
}
</script>
<template>
<HuiSpace wrap>
<HuiCountdown :value="targetTime" format="s.S" suffix="秒" @finish="finished = true" />
<HuiButton @click="restart">重新開始</HuiButton>
<span v-if="finished">時間到</span>
</HuiSpace>
</template>
掛載時目標時間已經過去,顯示 0 並立即觸發 finish:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCountdown, HuiSpace } from '@nscode/handy-ui'
// 1 分鐘前
const expired = Date.now() - 60 * 1000
const status = ref('進行中')
</script>
<template>
<HuiSpace wrap align="end">
<HuiCountdown :value="expired" title="報名截止" @finish="status = '已截止'" />
<span>{{ status }}</span>
</HuiSpace>
</template>
change 僅在格式化後的顯示文字變化時觸發,參數按天、時、分、秒、毫秒拆好。格式含有效的 S 毫秒欄位時按動畫幀檢查,其餘按最小顯示單位定時;下面只在秒數變化時更新獨立顯示:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCountdown } from '@nscode/handy-ui'
import type { CountdownTimeData } from '@nscode/handy-ui'
// 2 天 3 小時 4 分鐘後
const targetTime = Date.now() + ((2 * 24 + 3) * 60 + 4) * 60 * 1000
const timeData = ref<CountdownTimeData>()
const handleChange = (data: CountdownTimeData) => {
if (data.seconds !== timeData.value?.seconds) {
timeData.value = data
}
}
</script>
<template>
<HuiCountdown :value="targetTime" format="D 天 HH:mm:ss" @change="handleChange" />
<p v-if="timeData" style="margin-top: 12px">
days {{ timeData.days }},hours {{ timeData.hours }},minutes {{ timeData.minutes }},seconds
{{ timeData.seconds }},total {{ timeData.total }} 毫秒
</p>
</template>
停止和恢復刷新
透過 ref 呼叫 stopCountdown 可以讓顯示停在目前數值上,startCountdown 恢復刷新。這兩個方法只控制“是否刷新顯示”,目標時間不變,所以恢復後顯示的是當時真實的剩餘時間,不是從停下的數值接著走。要做“暫停後接著計時”,在恢復時把 value 改成“現在 + 暫停時的剩餘毫秒數”。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCountdown, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { CountdownExpose } from '@nscode/handy-ui'
const countdownRef = ref<CountdownExpose>()
const targetTime = Date.now() + 10 * 60 * 1000
</script>
<template>
<HuiSpace wrap>
<HuiCountdown ref="countdownRef" :value="targetTime" format="mm:ss" />
<HuiButton @click="countdownRef?.stopCountdown()">停止刷新</HuiButton>
<HuiButton @click="countdownRef?.startCountdown()">恢復刷新</HuiButton>
</HuiSpace>
</template>
“暫停後接著計時”的寫法:暫停時記下剩餘毫秒數並停止刷新,繼續時把 value 改成“現在 + 剩餘毫秒數”。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCountdown, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { CountdownExpose, CountdownTimeData } from '@nscode/handy-ui'
const countdownRef = ref<CountdownExpose>()
const targetTime = ref(Date.now() + 60 * 1000)
const paused = ref(false)
let remaining = 60 * 1000
const handleChange = (data: CountdownTimeData) => {
remaining = data.total
}
const pause = () => {
countdownRef.value?.stopCountdown()
paused.value = true
}
const resume = () => {
targetTime.value = Date.now() + remaining
paused.value = false
}
</script>
<template>
<HuiSpace wrap align="center">
<HuiCountdown ref="countdownRef" :value="targetTime" format="mm:ss.S" @change="handleChange" />
<HuiButton v-if="!paused" @click="pause">暫停</HuiButton>
<HuiButton v-else type="primary" @click="resume">繼續</HuiButton>
</HuiSpace>
</template>
獲取驗證碼
常見的“60 秒後重新獲取”:點按鈕後才掛載倒計時開始計時,期間按鈕禁用;到 0 時觸發 finish,卸載倒計時、按鈕恢復可點。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCountdown, HuiSpace } from '@nscode/handy-ui'
const target = ref<number>()
const send = () => {
target.value = Date.now() + 60 * 1000
}
</script>
<template>
<HuiSpace wrap align="center">
<HuiButton type="primary" :disabled="target !== undefined" @click="send">獲取驗證碼</HuiButton>
<HuiCountdown
v-if="target !== undefined"
:value="target"
format="s"
suffix="秒後可重新獲取"
:value-style="{ fontSize: 'var(--hui-font-size-base)' }"
@finish="target = undefined"
/>
</HuiSpace>
</template>
Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| value | number | Date | - | 目標時間,毫秒時間戳或 Date 物件,必填。不是有效時間時顯示 0,不觸發 finish |
| format | string | 'HH:mm:ss' | 顯示格式,見上面的“格式” |
| title | string | - | 標題 |
| prefix | string | - | 數值前面的文字 |
| suffix | string | - | 數值後面的文字 |
| valueStyle | Record<string, string | number> | - | 數值的行內樣式 |
Events
| 事件 | 回呼參數 | 說明 |
|---|---|---|
| finish | - | 倒計時到 0 時觸發一次。掛載時目標時間已經過去,也會立即觸發 |
| change | (timeData: CountdownTimeData) | 格式化顯示文字變化時觸發;不固定按每幀或每秒60次發出 |
Slots
| 插槽 | 作用域參數 | 說明 |
|---|---|---|
| title | - | 標題,優先於 title 屬性 |
| prefix | - | 數值前面的內容,優先於 prefix 屬性 |
| suffix | - | 數值後面的內容,優先於 suffix 屬性 |
方法
透過元件的 ref 呼叫。
| 方法 | 參數 | 回傳值 | 說明 |
|---|---|---|---|
| startCountdown | - | void | 按目前的目標時間重新開始刷新;目標時間已過時立即觸發 finish |
| stopCountdown | - | void | 停止刷新,顯示停在目前數值上 |
類型
公開方法使用CountdownExpose;舊方法類型僅保留deprecated相容別名CountdownInstance,將在1.0移除。
interface CountdownTimeData {
/** 剩餘天數 */
days: number
/** 去掉整天後剩餘的小時(0-23) */
hours: number
/** 去掉整小時後剩餘的分鐘(0-59) */
minutes: number
/** 去掉整分鐘後剩餘的秒(0-59) */
seconds: number
/** 去掉整秒後剩餘的毫秒(0-999) */
milliseconds: number
/** 剩餘的總毫秒數 */
total: number
}
interface CountdownExpose {
startCountdown: () => void
stopCountdown: () => void
}
/** @deprecated 請使用CountdownExpose,將在1.0移除。 */
type CountdownInstance = CountdownExpose