传入指定时间戳开始倒计时

时间:2021-12-10 22:08:38
var vm = new Vue({
    el:'#app',
    data: {
      time : new Date().getTime() + 24 * 60 * 60 * 1000,
      timer : null,
      countTimeObj: {},
      endTime : null
    },
    mounted () {
      var that = this
      that.countTime()
    },
    methods : {
      // 倒计时
      countTime () {
        var that = this;
        var start = new Date().getTime();
        function num (n) {
          return n < 10 ? ('0'+ n ) : n;
        }
        // 差值
        var leftTime = parseInt((that.time) - start);
        var h = (parseInt(leftTime / (60*60*1000) % 24));
        var m = (parseInt(leftTime / (60*1000) % 60));
        var s = (parseInt(leftTime / 1000 % 60));
        that.countTimeObj  = {
          h:num(h),
          m:num(m),
          s:num(s)
        };
        that.endTime = that.countTimeObj.h + ' : ' + that.countTimeObj.m + ' : ' + that.countTimeObj.s
        // console.log(that.endTime)
        // 时间差为0
        if (leftTime < 0) {
          that.countTimeObj = {
            h:"00",
            m:"00",
            s:"00"
          };
          clearTimeout(that.timer);
        } else {
          that.timer = setTimeout(that.countTime,1000);
        }
      }
    }
  })