vue如何清除定时器
Posted 梦彧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
})
以上是关于vue如何清除定时器的主要内容,如果未能解决你的问题,请参考以下文章