Countdown
Display the time remaining until a target and emit an event when it is reached. Useful for deadlines, verification-code retries, and time-limited actions.
Import
import { HuiCountdown } from '@nscode/handy-ui'
Basic Usage
Pass a target timestamp in milliseconds to value. The display uses “target time minus current time”, so it stays accurate after returning from a background tab or waking the computer.
<script setup lang="ts">
import { HuiCountdown } from '@nscode/handy-ui'
// One hour from now.
const targetTime = Date.now() + 60 * 60 * 1000
</script>
<template>
<HuiCountdown :value="targetTime" title="Time until the event ends" />
</template>
Calculate and store the target first. Do not write :value="Date.now() + 10000" in a template: every parent render would create a new target and restart the countdown.
Date Targets
value also accepts a Date object.
<script setup lang="ts">
import { HuiCountdown } from '@nscode/handy-ui'
// Midnight at the end of today.
const endOfToday = new Date()
endOfToday.setHours(24, 0, 0, 0)
</script>
<template>
<HuiCountdown :value="endOfToday" title="Time until today ends" />
</template>
Format
format controls the display and defaults to HH:mm:ss. Supported units:
| Token | Meaning | Description |
|---|---|---|
D | Days | D is unpadded; DD pads to 2 digits. |
H | Hours | H is unpadded; HH pads to 2 digits. |
m | Minutes | m is unpadded; mm pads to 2 digits. |
s | Seconds | s is unpadded; ss pads to 2 digits. |
S | Milliseconds | S shows hundreds of milliseconds (1 digit), SS tens (2 digits), and SSS milliseconds (3 digits). |
Other characters are displayed literally. Enclose text containing these token letters in square brackets, for example D [days] HH:mm:ss.
<script setup lang="ts">
import { HuiCountdown, HuiSpace } from '@nscode/handy-ui'
// One day, two hours, and three minutes from now.
const targetTime = Date.now() + ((24 + 2) * 60 + 3) * 60 * 1000
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiCountdown :value="targetTime" format="DD [days] HH:mm:ss" />
<HuiCountdown :value="targetTime" format="D [days] H [hours] m [min] s [sec]" />
<HuiCountdown :value="targetTime" format="HH[hours]mm[min]ss[sec]" />
<HuiCountdown :value="targetTime" format="D [days] HH:mm:ss" />
</HuiSpace>
</template>
Folding Omitted Units
Larger units omitted from the format are folded into the next smaller unit. For example, with 1 day, 2 hours, and 3 minutes remaining:
<script setup lang="ts">
import { HuiCountdown, HuiSpace } from '@nscode/handy-ui'
// One day, two hours, and three minutes from now.
const targetTime = Date.now() + ((24 + 2) * 60 + 3) * 60 * 1000
</script>
<template>
<HuiSpace direction="vertical" align="start">
<!-- 1 day 02:03:00 -->
<HuiCountdown :value="targetTime" format="D [days] HH:mm:ss" />
<!-- 26:03:00 -->
<HuiCountdown :value="targetTime" format="HH:mm:ss" />
<!-- 1563:00 -->
<HuiCountdown :value="targetTime" format="mm:ss" />
<!-- 93780 seconds -->
<HuiCountdown :value="targetTime" format="s" suffix=" seconds" />
</HuiSpace>
</template>
Milliseconds
An S field displays milliseconds: S shows hundreds (1 digit), SS tens (2 digits), and SSS milliseconds (3 digits).
<script setup lang="ts">
import { HuiCountdown, HuiSpace } from '@nscode/handy-ui'
// Ten minutes from now.
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, and Suffix
title, prefix, and suffix accept text. Use the matching slots for icons or other content; slots take precedence over props.
<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="Limited-time sale"
prefix="Remaining: "
suffix=" seconds"
format="s"
/>
<HuiCountdown :value="targetTime">
<template #title>Sale countdown</template>
<template #prefix>
<HuiIcon name="clock" />
</template>
<template #suffix> until it ends</template>
</HuiCountdown>
<HuiCountdown :value="targetTime" format="mm:ss">
<template #title>
<span style="color: var(--hui-color-danger)">Registration closes in</span>
</template>
</HuiCountdown>
</HuiSpace>
</template>
Value Styles
valueStyle adds inline styles to the value.
<script setup lang="ts">
import { HuiCountdown, HuiSpace } from '@nscode/handy-ui'
// One hour from now.
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="Danger color, tabular digits"
:value-style="{ color: 'var(--hui-color-danger)', fontFamily: 'var(--hui-font-family-mono)' }"
/>
<HuiCountdown
:value="targetTime"
title="Small and bold"
:value-style="{ fontSize: '16px', fontWeight: 600 }"
/>
</HuiSpace>
</template>
Events and Restarting
finish fires when the countdown reaches 0. Changing value starts counting toward the new target. A new target after completion can emit finish again.
<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=" seconds" @finish="finished = true" />
<HuiButton @click="restart">Restart</HuiButton>
<span v-if="finished">Time is up</span>
</HuiSpace>
</template>
If the target is already in the past on mount, the component displays 0 and immediately emits finish:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCountdown, HuiSpace } from '@nscode/handy-ui'
// One minute ago.
const expired = Date.now() - 60 * 1000
const status = ref('In progress')
</script>
<template>
<HuiSpace wrap align="end">
<HuiCountdown :value="expired" title="Registration deadline" @finish="status = 'Closed'" />
<span>{{ status }}</span>
</HuiSpace>
</template>
change fires only when the formatted display text changes. Its argument separates days, hours, minutes, seconds, and milliseconds. Formats with an active S field are checked on animation frames; other formats use a timer for the smallest displayed unit. This example updates the separate display only when the second changes:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCountdown } from '@nscode/handy-ui'
import type { CountdownTimeData } from '@nscode/handy-ui'
// Two days, three hours, and four minutes from now.
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 [days] 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 }} ms
</p>
</template>
Stopping and Resuming Display Updates
Call stopCountdown through a ref to freeze the display and startCountdown to resume updating it. These methods only control display refreshes; the target stays unchanged. Resuming shows the actual remaining time then, rather than continuing from the frozen value. To pause time itself, resume with value set to “now plus the milliseconds remaining when paused”.
<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()">Stop updates</HuiButton>
<HuiButton @click="countdownRef?.startCountdown()">Resume updates</HuiButton>
</HuiSpace>
</template>
For a resumable pause, store the remaining milliseconds and stop refreshing. On resume, set value to “now plus remaining milliseconds”.
<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">Pause</HuiButton>
<HuiButton v-else type="primary" @click="resume">Resume</HuiButton>
</HuiSpace>
</template>
Verification-Code Retry
For the common “retry after 60 seconds” pattern, mount the countdown only after clicking the button and disable the button while it runs. At 0, finish removes the countdown and enables the button.
<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"
>Get verification code</HuiButton
>
<HuiCountdown
v-if="target !== undefined"
:value="target"
format="s"
suffix=" seconds until retry"
:value-style="{ fontSize: 'var(--hui-font-size-base)' }"
@finish="target = undefined"
/>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value | number | Date | - | Required target time as a millisecond timestamp or Date. Invalid targets show 0 without emitting finish. |
| format | string | 'HH:mm:ss' | Display format; see Format above. |
| title | string | - | Title. |
| prefix | string | - | Text before the value. |
| suffix | string | - | Text after the value. |
| valueStyle | Record<string, string | number> | - | Inline styles for the value. |
Events
| Event | Arguments | Description |
|---|---|---|
| finish | - | Emitted once when reaching 0, including immediately on mount if the target is already past. |
| change | (timeData: CountdownTimeData) | Emitted when the formatted display changes, rather than at a fixed frame rate or 60 times per second. |
Slots
| Slot | Scope | Description |
|---|---|---|
| title | - | Title content, overriding title. |
| prefix | - | Content before the value, overriding prefix. |
| suffix | - | Content after the value, overriding suffix. |
Methods
Call these through the component ref.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| startCountdown | - | void | Resumes display updates for the current target; emits finish immediately if it has passed. |
| stopCountdown | - | void | Stops display updates at the current value. |
Types
Use CountdownExpose for public methods. CountdownInstance remains only as a deprecated compatibility alias and will be removed in 1.0.
interface CountdownTimeData {
/** Remaining days. */
days: number
/** Hours after removing whole days (0–23). */
hours: number
/** Minutes after removing whole hours (0–59). */
minutes: number
/** Seconds after removing whole minutes (0–59). */
seconds: number
/** Milliseconds after removing whole seconds (0–999). */
milliseconds: number
/** Total remaining milliseconds. */
total: number
}
interface CountdownExpose {
startCountdown: () => void
stopCountdown: () => void
}
/** @deprecated Use CountdownExpose; removed in 1.0. */
type CountdownInstance = CountdownExpose