Skip to content

CountDown 倒计时

用于实时展示倒计时数值,支持毫秒精度。

导入

js
import CountDown from '@/components/display/countdown/CountDown.vue';

使用方法

基础用法

vue
<template>
  <CountDown :time="1 * 60 * 60 * 1000" />
</template>

自定义格式

vue
<template>
  <CountDown :time="72 * 60 * 60 * 1000" format="DD 天 HH 时 mm 分 ss 秒" />
</template>

毫秒级渲染

vue
<template>
  <CountDown :time="1 * 60 * 60 * 1000" millisecond format="HH:mm:ss:SS" />
</template>

自定义渲染

vue
<template>
  <CountDown :time="1 * 60 * 60 * 1000">
    <template #renderText="{ time }">
      <FlexRow center>
        <text :style="{ backgroundColor: '#f00', color: '#fff', padding: '10rpx' }">{{time.hours}}</text>
        <text :style="{ margin: '0 8px' }">:</text>
        <text :style="{ backgroundColor: '#f00', color: '#fff', padding: '10rpx' }">{{time.minutes}}</text>
        <text :style="{ margin: '0 8px' }">:</text>
        <text :style="{ backgroundColor: '#f00', color: '#fff', padding: '10rpx' }">{{time.seconds}}</text>
      </FlexRow>
    </template>
  </CountDown>
</template>

手动控制

vue
<template>
  <CountDown ref="countdownRef" :time="1 * 60 * 60 * 1000" />
  <Button @click="countdownRef.start()" type="primary" text="开始" />
  <Button @click="countdownRef.stop()" type="default" text="暂停" />
  <Button @click="countdownRef.reset()" type="default" text="重置" />
</template>

<script setup lang="ts">
import { ref } from 'vue';

const countdownRef = ref();
</script>

API 参考

Props

名称说明类型必填默认值
time倒计时时长,单位毫秒number0
format时间格式string'HH:mm:ss'
autoStart是否自动开始倒计时booleantrue
millisecond是否开启毫秒级渲染booleanfalse

Slots

名称说明参数
renderText自定义倒计时显示内容{ time: { days: number, hours: number, minutes: number, seconds: number, milliseconds: number } }

Events

名称说明参数
finish倒计时结束事件-

实例方法

通过 ref 可以获取到 CountDown 实例并调用实例方法

方法名说明参数
start开始倒计时-
stop暂停倒计时-
reset重置倒计时time?: number