vue定时器清除不掉,导致功能频繁执行问题
前端Vue项目中定时器清除问题
·
前端Vue项目中定时器清除问题
方法1:常规使用和清除
clearInterval(this.timer)//使用前先清空定时器
this.timer = setInterval(()=>{
console.log(1)
}, 1000)
方法2:使用数组存储每一个定时器的标识,避免某些原因导致定时器未能清除 (如:页面重复初始化)
if(!window.timer){ window.timer = []}
// 将存起来的定时器一并清除
window.timer.map(item=>{
clearInterval(item)
})
const timerId = setInterval(()=>{
console.log(1)
}, 1000)
window.timer.push(timerId)
更多推荐

所有评论(0)