Preface Clear the timer. I believe quite a few people write it like this: export default { data() { reurn timer: null } }, mounted() { this.timer = setInterval(() => { console.log('setInterval') }, 2000) }, beforeDestroy() { clearInterval(this.timer) } } This is a common piece of code. At least several of my friends (with 1-3 years of experience) write it this way. There are three inelegant problems here:
optimization Directly on the code: export default { data() { reurn } }, mounted() { let timer = setInterval(() => { console.log('setInterval') }, 2000) this.$once('hook:beforeDestroy', () => { clearInterval(timer) timer = null }) } } Here, a hook is used to monitor the beforeDestroy life cycle, so that the timer only needs to be defined in the life cycle, and all the above problems are solved. Derivative question: beforeDestroy is not triggered? In the backend system, we often set up page caches. When the route is cached by keep-alive, the beforeDestroy life cycle is not followed. Therefore, some people think that clearing the timer in beforeDestroy is enough, and they don’t even check that the timer is not actually cleared. Knowing the reason is easy to solve, with the help of activated and deactivated hooks: export default { data() { reurn } }, mounted() { let timer = setInterval(() => { console.log('setInterval') }, 2000) this.$on('hook:activated', () => { if (timer === null) { // Avoid repeated timer activation timer = setInterval(() => { console.log('setInterval') }, 2000) } }) this.$on('hook:deactivated', () => { clearInterval(timer) timer = null }) } } It should be noted here that due to caching reasons, $on should be used instead of $once, otherwise it will not be triggered again after being executed once. This is the end of this article about how to elegantly clear the timer in Vue. For more relevant Vue clearing timer content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed steps to install a virtual machine and use CentOS 8 using VMware 15
>>: Monitor changes in MySQL table content and enable MySQL binlog
Table of contents 0. The kernel tree that comes w...
Preface Samba is a free software that implements ...
1. Write Shell script crontab.sh #!/bin/bash step...
I believe everyone is very sensitive to colors. C...
1. Background Netplan is a new command-line netwo...
Abstract: When people talk about MySQL performanc...
ElementUI implements a table tree list loading tu...
There are two files a.htm and b.htm. In the same d...
The question is referenced from: https://www.zhih...
The role of the interface: Interface, in English:...
This article describes how to install and configu...
Solution function mergeImgs(list) { const imgDom ...
Table of contents Avoid repetitive rendering loop...
Preface In today's increasingly convenient In...
This article shares the specific code of js to im...