24

Im building an automatic refreshing comment section for my website using jQuery .load. So I am using a javascript 'setTimeout' timer to check for new comments.

But after doing some stuff like changing comment pages or deleting (all using ajax), a few old timers keep running, even though I used clearTimeout before loading new ajax content.

Is there some way to clear ALL javascript timers when I load new ajax content?

4 Answers 4

43

Warning: My code bellow is problematic, mainly because the requirement is itself is problematic. I wonder why would you want to clear all timers any way. This makes your code breaking any plugin the uses the timers. Which is much more common than you may think, unless of course you're debugging or having fun, then it doesn't matter.


If you must:

This is one of the worst possible solutions. However it reveals a potential security vulnerability in JavaScript itself. But since it just works (clears ALL of the timers) I thought it would be cool to share it:

var maxId = setTimeout(function(){}, 0);

for(var i=0; i < maxId; i+=1) { 
    clearTimeout(i);
}
13
  • 4
    While the accepted answer is a cool best-practice, this answers the question posited in the title.
    – Joe Coder
    Dec 27, 2012 at 6:38
  • This is great solution! I guess it's cross-browser anyway?
    – 0xC0DEGURU
    Sep 11, 2013 at 15:20
  • 1
    @0xC0DEGURU Based on my experience, I can say yes. But you should try it. Anyway it's very bad to this! Why would you want to do that! Sep 15, 2013 at 12:41
  • @OmarIthawi As long as it works fine, it's not bad approach ;) Imagine there was in the JavaScript Standard function that do this same task. It would look exactly like the sample you gave.
    – 0xC0DEGURU
    Sep 17, 2013 at 8:17
  • 1
    I don't think there's anything in the standard stating that timer ids have to be incremented like that, so it's possible some browser may change it and cause that code to break. However, it's way more work to randomly generate id's and then keep track of them rather than just incrementing a counter. Dec 9, 2013 at 16:42
28

There's no general function in javascript that allows you to clear all timers. You will need to keep track of all timers you create. For this you could use a global array:

var timers = [];
...
// add a timer to the array
timers.push(setTimeout(someFunc, 1000));
...
// clear all timers in the array
for (var i = 0; i < timers.length; i++)
{
    clearTimeout(timers[i]);
}
4
  • 1
    The problem is that my newer loaded ajax content has no control over the old running timers.
    – Jens
    Dec 13, 2009 at 16:04
  • 1
    That's why I suggested a global variable. Dec 13, 2009 at 16:05
  • I've been struggling with this one. Brilliant! THanks Nov 15, 2013 at 2:46
  • while (id = timers.pop()) clearTimeout(id); <- I like this better, but makes little difference, the for loop is just as good, really - although I'm not allowed a var or let in front of the id declaration, which sucks - you could go with for(let id=timers.pop();id;id=timers.pop()) clearTimeout(id) instead, but then you lose the nicer look Jul 14, 2021 at 10:53
2

You may want to consider using jQuery Timers instead, which abstracts away many of the "ugly" details of setTimeout / setInterval, and makes them easier to use in your code for things like what you are describing.

2
  • @Benubird - Unfortunately that is the most recent one I can find. If you have a newer one, please let me know... Mar 27, 2013 at 14:37
  • Yeah, looks like there is no current version (stackoverflow.com/questions/3674566/…), might not be usable any more.
    – Benubird
    Mar 27, 2013 at 14:41
1

This might help. I have a case that user is the one who is creating the timers, something like an open platform for development, obviously I don't have access to ID of those timers. So I need to sandbox them and remove them all if necessary. To do that I create a hidden <iframe> and load all timers in that iframe. To remove all timers, simply remove the iframe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.