当前位置: 代码迷 >> 综合 >> JS 前端同时设置多个定时器
  详细解决方案

JS 前端同时设置多个定时器

热度:35   发布时间:2024-01-31 08:27:16.0
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;  // 时间递减}}

 

  相关解决方案