需求:
设置一个活动抢购倒计时- 分:秒:毫秒
实现
timeOut () {
let times = 10 * 60 * 1000 // 10分钟倒计时
const timeId = setInterval(() => {
if (times <= 0) {
clearInterval(timeId)
// 防止最后出现负数(因为每次减的不是1ms)
this.m = '00'
this.s = '00'
this.ss = '00'
return
}
times = times - 11
const m = parseInt(times / 1000 / 60)
const s = parseInt(times / 1000 % 60)
// 为了保证毫秒不大于2位数
const ss = parseInt(times % 1000 / 11)
this.m = String(m).length > 1 ? String(m) : 0 + String(m)
this.s = String(s).length > 1 ? String(s) : 0 + String(s)
this.ss = String(ss).length > 1 ? String(ss) : 0 + String(ss)
}, 11)
},