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

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);
       })
  

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理