翻譯進行中
本頁尚未翻譯為繁體中文,以下顯示簡體中文原文。範例的預設元件文字仍使用繁體中文。
CountDown 倒计时
按截止时刻计算剩余时间,避免累计定时误差;支持手动暂停和重置。
引入
uni-app 配置 easycom 后使用 <hui-count-down>。公开类型和函数从 @nscode/handy-uni 导入;普通 Vue 3 安装 @nscode/handy-uni/web 预览插件。
示例
示例可独立运行;交互结果直接显示在页面。
vue
<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
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
value | number | Date | — | 绝对截止时间,使用毫秒时间戳或 Date;优先于 time。 |
time | number | — | 时长,ms;从开始计时起倒数。value 和 time 都传时用 value 并警告 |
format | string | 'HH:mm:ss' | D、H、m、s、S,重复表示补零位数,方括号原样输出;没出现的大单位折进下一级 |
autoStart | boolean | true | 挂载以及 value、time、format 变化或 reset 后自动开始。 |
now | () => number | — | 取当前时间(接服务器时间差用);调用时才读。不传用 Date.now |
根节点类名和样式使用 customClass、customStyle。尺寸数字按 px,时长除明确说明外按 ms。
Events
| 事件 | 参数 |
|---|---|
change | (current: CountDownTimeData) |
finish | () |
受控状态由父级回写更新事件后改变。多词事件使用短横线。禁用操作、取消及过期异步结果不提交成功变更。
Slots
| 插槽 | 作用域 |
|---|---|
default | { current, text } |
方法
| 方法 | 签名 | 说明 |
|---|---|---|
start | (): void | 开始或继续倒计时 |
pause | (): void | 暂停并保存剩余状态 |
reset | (): void | 按当前属性重置 |
同步导出 useCountDown,返回 current、start、pause、reset;target 传绝对截止时刻,time 传初始时长,precision 为正毫秒数或 frame。
类型
CountDownProps、CountDownEmits、CountDownExpose.
CountDownTimeData、UseCountDownOptions.
平台说明
value 优先于 time。时长模式显式 pause 会冻结剩余时长;截止时刻模式恢复时追赶真实截止时间。KeepAlive 失活只停刷新,恢复后追赶;format 用方括号包住字面量。useCountDown 同步导出,参数为 time、target、precision、now、onChange、onFinish。
运行时读屏名称按分钟更新,归零时同步刷新;重置以及 value、time、format 变化也会立即更新读屏名称,即使 autoStart=false。
设备冒烟清单.