@neosjs/countdown
倒计时组件。按 format 模板渲染剩余时间,支持毫秒级渲染、自动开始、暂停/继续/重置,并可通过默认插槽完全自定义渲染内容。
安装
sh
pnpm add @neosjs/countdownsh
npm i @neosjs/countdownsh
yarn add @neosjs/countdown快速上手
time 为倒计时总时长(毫秒),默认格式 HH:mm:ss。
vue
<script setup lang="ts">
import { CountDown } from '@neosjs/countdown'
</script>
<template>
<CountDown :time="60 * 60 * 1000" format="HH:mm:ss" />
</template>自定义渲染
使用默认插槽接管渲染,作用域参数为当前时间对象(含 total、days、hours、minutes、seconds、milliseconds 字段)。
vue
<CountDown :time="30 * 1000">
<template #default="{ hours, minutes, seconds }">
<span class="custom">{{ hours }}时{{ minutes }}分{{ seconds }}秒</span>
</template>
</CountDown>控制倒计时
auto-start 为 false 时,可通过 ref 手动调用 start / pause / reset。
vue
<script setup lang="ts">
import { CountDown } from '@neosjs/countdown'
import { ref } from 'vue'
const countDownRef = ref()
</script>
<template>
<CountDown ref="countDownRef" :time="60 * 1000" :auto-start="false" />
<button @click="countDownRef.start()">
开始
</button>
<button @click="countDownRef.pause()">
暂停
</button>
<button @click="countDownRef.reset()">
重置
</button>
</template>Props
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| time | number | string | 0 | 倒计时总时长(毫秒) |
| format | string | 'HH:mm:ss' | 时间格式模板,如 HH:mm:ss:SSS、DD天HH时mm分ss秒 |
| auto-start | boolean | true | 是否在挂载后自动开始倒计时 |
| millisecond | boolean | false | 是否开启毫秒级渲染 |
Emits
| 事件名 | 参数 | 说明 |
|---|---|---|
| onChange | (current: CurrentTime) | 倒计时变化时触发 |
| onFinish | — | 倒计时结束时触发 |
| onStart | — | 倒计时开始时触发 |
| onPause | — | 倒计时暂停时触发 |
Slots
| 插槽名 | 参数 | 说明 |
|---|---|---|
| default | current: CurrentTime | 自定义渲染内容;不传时显示格式化文本 |
ts
interface CurrentTime {
total: number // 剩余总毫秒数
days: number
hours: number
minutes: number
seconds: number
milliseconds: number
}方法
通过 ref 可调用以下方法:
| 方法名 | 参数 | 说明 |
|---|---|---|
| start | — | 开始倒计时 |
| pause | — | 暂停倒计时 |
| reset | — | 重置到 time 并按 auto-start 决定是否重新开始 |