After the page cache in Vue is destroyed, it becomes invalid. How to close the timer?

Scenes

A timer is set on the page. If the component is destroyed without closing the timer, it will continue to execute, which will consume very much performance, so the timer needs to be closed in time.

off timer

In the vue project, under normal circumstances, we can close it in the destroyed life cycle. Once keep-alive is used for caching in the page, destroyed will become invalid at this time. It needs to be closed in the deactivated hook function, which is a hook function unique to keep-alive.

code

No cached page:

destroyed(){
	clearInterval(this.timer)
}

Cached pages:

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


————————————————
Original link: https://blog.csdn.net/lifangfang0607/article/details/112321649

Guess you like

Origin blog.csdn.net/qq_42351675/article/details/123404055