CountDown
Calculates remaining time from a deadline to avoid accumulated timer drift; supports pause and reset.
Import
Use <hui-count-down> after configuring uni-app easycom. Import public types and functions from @nscode/handy-uni. Ordinary Vue applications install the @nscode/handy-uni/web preview plugin.
Examples
Each example is standalone. Controls show the current value or event result on the page.
<template>
<view>
<hui-space direction="vertical" fill>
<hui-count-down
ref="countdown"
custom-class="hui-space__item"
:time="90000"
format="mm:ss"
:auto-start="false"
@change="(value) => (lastEvent = String(value.total))"
@finish="lastEvent = 'finish'"
/>
<hui-count-down
custom-class="hui-space__item"
:time="3723000"
format="HH[时/h] mm[分/m] ss[秒/s]"
/>
<hui-count-down custom-class="hui-space__item" :time="5000" format="ss.SSS"
><template #default="{ text }"
><hui-tag>{{ text }}</hui-tag></template
></hui-count-down
>
<view class="hui-space__item"
><hui-space
><hui-button custom-class="hui-space__item" @click="countdown?.start()"
>开始 / Start</hui-button
><hui-button custom-class="hui-space__item" @click="countdown?.pause()"
>暂停 / Pause</hui-button
><hui-button custom-class="hui-space__item" @click="countdown?.reset()"
>重置 / Reset</hui-button
></hui-space
></view
>
<text class="hui-space__item">{{ lastEvent }}</text>
</hui-space>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件 / No event')
import type { CountDownExpose } from '@nscode/handy-uni'
const countdown = ref<CountDownExpose>()
</script>
Props
| Prop | Type | Default | Details |
|---|---|---|---|
value | number | Date | — | Absolute deadline as a timestamp or Date; takes priority over time. |
time | number | — | Initial duration in milliseconds. |
format | string | 'HH:mm:ss' | Formats the displayed progress or duration. |
autoStart | boolean | true | Starts automatically after initialization or reset. |
now | () => number | — | Optional clock function, useful for server-time calibration. |
Root classes and styles use customClass and customStyle. Numbers representing dimensions use px; durations use ms unless explicitly documented.
Events
| Event | Arguments |
|---|---|
change | (current: CountDownTimeData) |
finish | () |
Controlled state only changes when the parent accepts its update event. Events with multiple words use kebab-case. Disabled or stale asynchronous actions do not emit a successful state change.
Slots
| Slot | Scope |
|---|---|
default | { current, text } |
Methods
| Method | Signature | Description |
|---|---|---|
start | (): void | Start or resume. |
pause | (): void | Pause and preserve remaining state. |
reset | (): void | Reset from current props. |
useCountDown is also exported and returns current, start, pause and reset. Use target for an absolute deadline or time for an initial duration. precision accepts a positive millisecond value or frame.
Types
CountDownProps、CountDownEmits、CountDownExpose.
CountDownTimeData、UseCountDownOptions.
Platform notes
value takes priority over time. Explicit pause freezes a duration, while an absolute target catches up to its deadline on resume. KeepAlive deactivation stops refreshes but does not extend the deadline. Brackets escape format literals. The exported useCountDown accepts time, target, precision, now, onChange and onFinish.
The accessible label refreshes at minute boundaries and at zero. Resetting or changing value, time or format also refreshes the label immediately, including when autoStart=false.
Device smoke checklist.