Vue Clears all JS timers

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.