vue倒计时功能

最近在写一个考试的h5页面时,有一个功能是考试时间限制,是一个倒计时的样式,直接上图:

考试时间不固定,根据后台返回的值来进行倒计时

<span class=""> <sm-icon name="alarm"></sm-icon> {{ time }}</span>

  
time = "";
timer = null;
remainingTime = 2000000; // 毫秒数


mounted() {
  this.start();
}


/**
   * @description: 倒计时
   * @return {*}
   */
  start() {
    //
    this.remainingTime = 1 * (60 * 60 * 1000) + 30 * (60 * 1000) + 0 * 1000;
    const endTime = Date.now() + this.remainingTime;
    this.timer = setInterval(() => {
      const remainingTime = endTime - Date.now();
      if (remainingTime <= 0) {
        clearInterval(this.timer);
        this.time = "00:00:00";
      } else {
        const hours = Math.floor(remainingTime / 3600000);
        const minutes = Math.floor((remainingTime % 3600000) / 60000);
        const seconds = Math.floor((remainingTime % 60000) / 1000);
        this.time = `${this.formatTime(hours)}:${this.formatTime(minutes)}:${this.formatTime(
          seconds,
        )}`;
      }
    }, 1000);
  }
  stop() {
    clearInterval(this.timer);
  }
  formatTime(time) {
    return time < 10 ? `0${time}` : time;
  }

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值