首页 > 其他 > 详细

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

时间:2019-05-07 10:04:33      阅读:266      评论:0      收藏:0      [点我收藏+]
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);
        }
      }
    }
  })

 

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

原文:https://www.cnblogs.com/MaxBlue/p/10823731.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!