vue 写一个简易的倒计时

407 阅读1分钟

首先需要从后端获取订单时间

let orderDate =new Date(res.data.data.created_at)

计算毫秒值

let nowTime = new Date().getTime(); let orderDate =new Date(res.data.data.created_at); let times =orderDate - parseInt(nowTime / 1000);

倒计时

changeTime(num) {
  var hours = parseInt(countDownTime/60/60);
  var minutes =parseInt(num/60%60);
  var seconds =parseInt(num%60);
  this.timeTxt = `${hours}${minutes}${seconds}秒`;
},

先获取当前倒计时分秒,然后开启定时器

`if (times <= 0) { this.orderStatus = false; }

          this.changeTime(times);
          this.timer = setInterval(() => {
            times-=1;
            this.changeTime(times);
            if (times <= 0) {
              clearInterval(this.timer);
              this.orderStatus = false;
            }
          }, 1000);