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
ps: Here is how to disable remote login of root a...
The same server simulates the master-slave synchr...
Table of contents 1. Resource download 2. Unzip t...
[LeetCode] 180. Consecutive Numbers Write a SQL q...
Preface This article mainly introduces the releva...
1. Docker installation and settings #Install Cent...
The most common mistake made by many website desi...
VMware Tools is a tool that comes with VMware vir...
1: Throughput (Requests per second) A quantitativ...
1. Enter the following address in the browser htt...
Copy code The code is as follows: <!DOCTYPE ht...
background As we all know, nginx is a high-perfor...
Call How to call Amap API? The official open docu...
JavaScript can do a lot of great things. This art...
Table of contents 1. Simple retrieval of data 2. ...