关于vue.js:vue如何清除定时器

34次阅读

共计 783 个字符,预计需要花费 2 分钟才能阅读完成。

1. 定义全局变量

 data() {
        return {
            textClose: ""m:'',// 格式化后的分
            s:'',// 格式化后的秒
        };
    },

2. 调用之前先革除定时器

  let examMinute = 90; // 90 FENZHOGN
  let tolTimers = examMinute * 60 * 1000;
  let tolS = tolTimers;
  const mm =  Math.floor(tolTimers / 1000 / 60);
  const ss = Math.floor((tolTimers / 1000) % 60);   
  that.m = mm > 9 ? mm : '0' + mm;
  that.s = ss> 9 ? ss: '0' +ss;
   // 革除定时器
   that.textClose && clearInterval(that.textClose);
   that.textClose = null;
   
   that.textClose = window.setInterval(() => {if (tolS<= 1000) { // 倒计时 
        that.automaticSubmit = true;
        window.clearInterval(that.textClose);
     }else{
       tolS-= 1000;
       // console.log('tolS',tolS);                               
         if(tolS<= 5* 60 * 1000){that.countDownFlag = true;}
         const mm =  parseInt((tolS / 1000 / 60))
         const ss = parseInt(tolS / 1000  % 60)                          
         that.m = mm > 9 ? mm : '0' + mm;
         that.s =ss> 9 ? ss: '0' +ss;
        }
      }, 1000);
      // 销毁定时器
      that.$once('hook:beforeDestroy', ()=>{clearInterval(that.textClose);
       })
  

正文完
 0