Vue Clears all JS timers
How to clear all the timers in the Webpack + Vue project when the page jumps
JS Timer will have a return value (number), through this return value we can find this timer
The Beforeeach method of the route guard can be used in the Vue project to clear the function
First, declare a global variable array, put all the timer's return values into the array,
(because the value returned by the timer increases with the number of calls, it is not possible to determine what the return value is, only to receive the return value, and then to operate.) )
Then every time to jump into the new page, through the Beforeeach method, the old page of the timer to clear all
Declaring an array, receiving the return value of the timer
Const TIMERCOUNT = [];
Set timer
Timercount[0] = setinterval (function () {
//....
},1000)
TIMERCOUNT[1] = setinterval (function () {
//....
},1000)
Entry file
Router.beforeeach (to, from, next) = = {
for (Let i = 0; I <= timercount.length; i++) {
Clearinterval (Timercount[i]);
}
Timercount.splice (0, Timercount.length)
Next ()
})
Use Clearinterval (i) to clear the specified timer, where parameter i is the return value of the timer
It clears the array every time, and of course, it doesn't make any difference.
In this way, all the timers on the page are cleared.
Vue Clears all JS timers