设置一个活动倒计时

   日期:2020-09-13     浏览:147    评论:0    
核心提示:需求: 写一个活动倒计时功能获取当前时间戳const current_time = Math.round(new Date().getTime() / 1000).toString()大概过程后台会返回一个活动开始时间的时间戳,你需要把活动时间戳减去当前时间戳, 再将剩下的转换为时分秒初始化的时候 定义一个定时器 clearInterval(this.newTimer) this.newTimer = setInterval(this.CountDown, 1000)method
需求: 写一个活动倒计时功能

获取当前时间戳
const current_time = Math.round(new Date().getTime() / 1000).toString()

大概过程
后台会返回一个活动开始时间的时间戳,你需要把活动时间戳减去当前时间戳,  再将剩下的转换为时分秒

初始化的时候 定义一个定时器
 clearInterval(this.newTimer)
 this.newTimer = setInterval(this.CountDown, 1000)


methods:{ 
// 给数子补 0
    timeAdd(str) { 
      if (str.length <= 1) { 
        str = '0' + str
      }
      return str
 },
CountDown(){ 
 if (this.current_time >= 0) { 
        let hour = Math.floor(this.current_time / 3600)
        let minutes = Math.floor((this.current_time / 60) % 60)
        let seconds = Math.floor(this.current_time % 60)
        if (hour === 0 && minutes === 0 && seconds === 0) { 
          // this.$alert('活动开始')
        }
        --this.maxtime
        this.msg =
          '活动剩余' +
          this.timeAdd(String(hour)) +
          '时' +
          this.timeAdd(String(minutes)) +
          '分' +
          this.timeAdd(String(seconds)) +
          '秒'
      } else { 
        clearInterval(this.newTimer)
        this.msg = '活动结束'
      }
    },
	}
}
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服