需求: 写一个活动倒计时功能
获取当前时间戳
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 = '活动结束'
}
},
}
}
设置一个活动倒计时
最新推荐文章于 2022-09-14 10:36:57 发布