vue中页面缓存后destroyed失效,如何关闭定时器

场景

页面中设置了定时器,如果组件销毁是没有关闭定时器,他还会一直执行,会非常耗性能,所以需要及时关闭定时器。

关闭定时器

vue项目中,正常情况下,我们在生命周期 destroyed 中关闭即可,一旦页面中使用了keep-alive 进行缓存,此时 destroyed 会失效。需要在 deactivated 钩子函数去关闭,他是 keep-alive 特有的钩子函数。

代码

没有缓存页面:

destroyed(){
	clearInterval(this.timer)
}

缓存过的页面:

// 开启定时器
activated(){
	this.start()
},
// 关闭定时器
deactivated(){
	clearInterval(this.timer)
}


————————————————
原文链接:https://blog.csdn.net/lifangfang0607/article/details/112321649

猜你喜欢

转载自blog.csdn.net/qq_42351675/article/details/123404055
今日推荐