加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 01:17:34 所属栏目:Linux 来源:网络整理
导读: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 = thi
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);
        }
      }
    }
  })

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读