Countdown 倒计时
实时倒计时:墙钟差值驱动(后台切换不漂移),支持受控直显与非受控自走两种模式, 格式化令牌 DD / HH / mm / ss / S。
基础用法
基础用法
02:00:0001:30
vue
<script setup lang="ts">
import { ref } from 'vue'
import { LhCountdown } from '@lh-ui/vue3'
const time = ref(2 * 3600 * 1000)
</script>
<template>
<div class="demo-row">
<LhCountdown :time="time" />
<LhCountdown :time="90 * 1000" format="mm:ss" />
</div>
</template>vue
<script>
import { LhCountdown } from '@lh-ui/vue3'
export default {
components: { LhCountdown },
}
</script>
<template>
<div class="demo-row">
<LhCountdown :time="2 * 3600 * 1000" />
<LhCountdown :time="90 * 1000" format="mm:ss" />
</div>
</template>受控直显
传入 v-model 后组件不自驱动,由外部负责推进(适合配合自己的时钟源)。
受控直显
00:01:00
vue
<script setup lang="ts">
import { ref } from 'vue'
import { LhCountdown, LhButton } from '@lh-ui/vue3'
const controlled = ref(65_000)
</script>
<template>
<div class="demo-row">
<LhCountdown v-model="controlled" />
<LhButton size="small" @click="controlled = Math.max(0, controlled - 1000)">-1s</LhButton>
</div>
</template>vue
<script>
import { LhCountdown, LhButton } from '@lh-ui/vue3'
export default {
components: { LhCountdown, LhButton },
data() {
return { controlled: 65_000 }
},
}
</script>
<template>
<div class="demo-row">
<LhCountdown v-model="controlled" />
<LhButton size="small" @click="controlled = Math.max(0, controlled - 1000)">-1s</LhButton>
</div>
</template>天 + 毫秒格式与结束回调
DD 天 + 毫秒段
01天02:00:01.500
vue
<script setup lang="ts">
import { LhCountdown } from '@lh-ui/vue3'
</script>
<template>
<LhCountdown :time="26 * 3600 * 1000 + 1500" format="DD天HH:mm:ss.S" millisecond />
</template>vue
<script>
import { LhCountdown } from '@lh-ui/vue3'
export default {
components: { LhCountdown },
}
</script>
<template>
<LhCountdown :time="26 * 3600 * 1000 + 1500" format="DD天HH:mm:ss.S" millisecond />
</template>Countdown API
Props
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| v-model / modelValue | 剩余毫秒(受控直显:组件不自驱动) | number | — |
| time | 非受控总时长 ms(挂载后 autoStart 自走) | number | — |
| format | 格式化串(令牌 DD/HH/mm/ss/S;无 DD 时 HH 承载总小时数) | string | 'HH:mm:ss' |
| autoStart | 非受控模式挂载后自动开始 | boolean | true |
| millisecond | 逐帧刷新(毫秒位显示时建议开启) | boolean | false |
| disabled | 禁用(灰化并暂停计时) | boolean | false |
| ariaLabel | 无障碍名称(未传取格式化文案) | string | — |
| sx | 快速布局属性 | LhSxProps | — |
Events
| 属性名 | 说明 | 类型 |
|---|---|---|
| update:modelValue | 受控值变化 | (value: number) |
| change | 剩余毫秒变化(非受控每步回调) | (value: number) |
| finish | 倒计时归零 | () |
方法(ref)
| 方法 | 说明 |
|---|---|
| start() | 开始 / 恢复(暂停点续走) |
| pause() | 暂停(记住剩余) |
| reset() | 重置回 time 总时长(autoStart 时立即重启) |
无障碍
- 根元素
role="timer",可达名称取ariaLabel或格式化文案

