time = 10; //前端显示的倒计时时间
pageTimer = {}; // 计时器数组
timerNumber = 0; // 计时器id
// 显示倒计时,点击触发倒计时:
start() {
this.time = 10;
this.pageTimer[this.timerNumber] = setInterval(() => {
this.countDown();
}, 1000);
this.timerNumber = this.timerNumber + 1;
}
// 判断计时是否结束
countDown() {
if (this.time === 0) {
//等于0时清除计时
if (JSON.stringify(this.pageTimer) !== "{}") { //判断计时器是否为空
for (var each in this.pageTimer) {
window.clearInterval(this.pageTimer[each]);
}
}
} else {
this.time = this.time - 1; // 时间递减
}
}