useCountDown

介绍

提供倒计时管理能力。

代码演示

基本用法

  1. <span>总时间:{{ current.total }}</span>
  2. <span>剩余天数:{{ current.days }}</span>
  3. <span>剩余小时:{{ current.hours }}</span>
  4. <span>剩余分钟:{{ current.minutes }}</span>
  5. <span>剩余秒数:{{ current.seconds }}</span>
  6. <span>剩余毫秒:{{ current.milliseconds }}</span>
  1. import { useCountDown } from '@vant/use';
  2. export default {
  3. setup() {
  4. const countDown = useCountDown({
  5. // 倒计时 24 小时
  6. time: 24 * 60 * 60 * 1000,
  7. });
  8. // 开始倒计时
  9. countDown.start();
  10. return {
  11. current: countDown.current,
  12. };
  13. },
  14. };

毫秒级渲染

倒计时默认每秒渲染一次,设置 millisecond 选项可以开启毫秒级渲染。

  1. import { useCountDown } from '@vant/use';
  2. export default {
  3. setup() {
  4. const countDown = useCountDown({
  5. time: 24 * 60 * 60 * 1000,
  6. millisecond: true,
  7. });
  8. countDown.start();
  9. return {
  10. current: countDown.current,
  11. };
  12. },
  13. };

API

类型定义

  1. type CurrentTime = {
  2. days: number;
  3. hours: number;
  4. total: number;
  5. minutes: number;
  6. seconds: number;
  7. milliseconds: number;
  8. };
  9. type CountDown = {
  10. start: () => void;
  11. pause: () => void;
  12. reset: (totalTime: number) => void;
  13. current: ComputedRef<CurrentTime>;
  14. };
  15. type UseCountDownOptions = {
  16. time: number;
  17. millisecond?: boolean;
  18. onChange?: (current: CurrentTime) => void;
  19. onFinish?: () => void;
  20. };
  21. function useCountDown(options: UseCountDownOptions): CountDown;

参数

参数说明类型默认值
time倒计时时长,单位毫秒number-
millisecond是否开启毫秒级渲染booleanfalse
onChange倒计时改变时触发的回调函数(current: CurrentTime) => void-
onFinish倒计时结束时触发的回调函数-

返回值

参数说明类型
current当前剩余的时间CurrentTime
start开始倒计时() => void
pause暂停倒计时() => void
reset重置倒计时,支持传入新的倒计时时长(time?: number): void

CurrentTime 格式

名称说明类型
total剩余总时间(单位毫秒)number
days剩余天数number
hours剩余小时number
minutes剩余分钟number
seconds剩余秒数number
milliseconds剩余毫秒number

useCountDown - 图1