Skip to content

Countdown 倒计时

介绍

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

基础用法

html
<template>
  <nut-countdown :end-time="end"></nut-countdown>
</template>
ts
const end = ref(Date.now() + 60 * 1000);

自定义格式

通过设置 format 属性可实现不同的倒计时展示文本。

html
<template>
  <nut-countdown :end-time="end" format="DD 天 HH 时 mm 分 ss 秒"></nut-countdown>
</template>

毫秒级渲染

html
<template>
  <nut-countdown :end-time="end" millisecond format="HH:mm:ss:SS"></nut-countdown>
</template>

以服务端的时间为准

html
<template>
  <nut-countdown :start-time="serverTime" :end-time="end"></nut-countdown>
</template>
ts
const serverTime = ref(Date.now() - 20 * 1000);
const end = ref(Date.now() + 60 * 1000);

异步更新结束时间

html
<template>
  <nut-countdown :end-time="asyncEnd"></nut-countdown>
</template>
ts
const asyncEnd = ref(0);

// 模拟异步时间
setTimeout(() => {
  asyncEnd.value = Date.now() + 30 * 1000;
}, 3000);

控制开始和暂停倒计时

通过 paused 属性实现倒计时的暂停和重启。

html
<template>
  <nut-countdown
    :end-time="end"
    :paused="paused"
    @on-paused="onPaused"
    @on-restart="onRestart"
  ></nut-countdown>

  <nut-button @click="toggle()">{{ paused ? "start" : "pause" }}</nut-button>
</template>
ts
const end = ref(Date.now() + 60 * 1000);
const paused = ref(false);

function toggle() {
  paused.value = !paused.value;
}

function onPaused(value) {
  console.log("paused", value);
}

function onRestart(value) {
  console.log("restart", value);
}

自定义展示样式

html
<template>
  <nut-countdown v-model="restTime" :end-time="end">
    <text>{{ restTime.d }}天</text>
    <text>{{ restTime.h }}</text>
    <text>:</text>
    <text>{{ restTime.m }}</text>
    <text>:</text>
    <text>{{ restTime.s }}</text>
  </nut-countdown>
</template>
ts
const restTime = ref({
  d: "1",
  h: "00",
  m: "00",
  s: "00"
});

const end = ref(Date.now() + 60 * 1000);

手动控制

通过 ref 获取到组件实例后,可以调用 startpausereset 方法。 在使用手动控制时,通过 time 属性实现倒计时总时长(单位:ms),startTimeendTime 属性失效。

html
<template>
  <nut-countdown
    ref="countdownEl"
    time="20000"
    format="ss:SS"
    :auto-start="false"
  ></nut-countdown>

  <nut-button @click="start()">开始</nut-button>
  <nut-button @click="pause()">暂停</nut-button>
  <nut-button @click="reset()">重置</nut-button>
</template>
ts
import type { CountdownInst } from "nutui-uniapp";

const countdownEl = ref<CountdownInst>();

function start() {
  countdownEl.value.start();
}

function pause() {
  countdownEl.value.pause();
}

function reset() {
  countdownEl.value.reset();
}

API

Props

参数说明类型可选值默认值
v-model当前时间,自定义展示内容时有效object-{}
start-time开始时间string / number-Date.now()
end-time结束时间string / number-Date.now()
format时间格式string-HH:mm:ss
millisecond是否开启毫秒级渲染boolean-false
auto-start是否自动开始倒计时boolean-true
time倒计时显示时间(单位:ms)(auto-startfalse 时有效)string / number-0
paused是否暂停boolean-false

时间格式

参数说明
DD
HH小时
mm分钟
ss
S毫秒(1 位)
SS毫秒(2 位)
SSS毫秒(3 位)

Events

事件名说明类型
on-end倒计时结束() => void
on-paused倒计时暂停(value: number) => void
on-restart倒计时开始(value: number) => void

Exposes

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

名称说明类型
start开始倒计时() => void
pause暂停倒计时() => void
reset重置倒计时(若 auto-starttrue,重置后会自动开始倒计时)() => void

主题定制

样式变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 组件

名称默认值
--nut-countdown-displayflex
--nut-countdown-colorinherit
--nut-countdown-font-sizeinitial

MIT Licensed