Click here to Skip to main content
14,643,162 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi, I have a javascript in which I will call a function every 1 minutes. My codes as below:
var clearTimer = setInterval(function ()
{
    MyMethod;
}, 60000);

Then after doing some stuff, I will clearInterval as below:
clearInterval(clearTimer);
clearTimer = 0;

But one thing which is weird where the html page hit the interval timer, it will refresh multiple times before displaying the page and the refreshing rate keeps on increasing on each time it refresh.

What I have tried:

Tried to use clear interval but the result is same.
Posted
Updated 3-Jun-16 1:26am
Comments
   
First of all, I would question why do you do that repeated call. What is the purpose?
Maybe you rather lack such thing as server push...
—SA

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I have found the solution.
I have to change the script into having IF statement as below:
if (clearTimer == 0) {
    clearTimer = setInterval(function () {
        MyMethod;
    }, 60000);
}


Move the clearTimer variable to become global variable and make it inside the IF statement to check if it is 0 then only call the setInterval function.
Previously on why does the codes keep on looping was due to there is no condition checking and every time the method is being called, it will add a recursive looping and will keep on accumulate to infinite number. Therefore the page will keep on refreshing as the setinterval is being called multiple times.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100