欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-18 11:01:56
var vm = new Vue({ el:'#app', data: { time : new Date().getTime() + 24 * 60 * 60 * 1000, timer : null, countTimeObj: {}, endTime : null }, mounted () ... ......
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);
        }
      }
    }
  })