views:

195

answers:

4

Does the browser keep track of active setInterval and setTimeout IDs? Or is this solely up to the developer to keep track of?

If it does keep track of them, is it accessible via the BOM?

A: 
scunliffe
Uhh ... I have very serious doubts about that ...
Pointy
definitely works, I've used this for over 10 years.
scunliffe
This works, but I pass the object "clearTimeout(timerHandle)" instead of name of the object "clearTimeout('timerHandle')". Haven't investigated which is the preferred method.
s_hewitt
@s_hewitt - yeah both work. In a more complex example I usually create the timer as an expando property on some object to keep it out of the global scope, and then remove it calling it with a variable reference instead of a string
scunliffe
From http://www.w3.org/TR/2009/WD-html5-20090212/no.html (yes, its a draft, but w3schools and http://www.w3.org/TR/Window/ explain it almost the same way) - setTimeout and setInterval return a long and clearTimeout/clearInterval accept a long to find and cancel.
s_hewitt
More to the OP's question - I couldn't find anything that gives access to the internal list of active timeouts / intervals or describes how to store them. I assume this is because each browser will implement it in their own way.
s_hewitt
@s_hewitt correct, you need to pass the number, not the string. I've tested the code example above, and it fails in IE8 and Firefox. Perhaps it worked in an old version of IE. Here is the code that does not work: http://pastebin.com/ntkrX113 Remove the quotes around 'pollHandle' and then it is fine.
Douglas
I'll have to take a look at this later, but I'm 500% positive it works (unless I've made a glaring typo)...
scunliffe
+1  A: 

This may interest you, if you are curious about how the timer is 'remembered' by its window.

<!doctype html> 
<html lang= "en"> 
<head> 
<meta charset= "utf-8"> 
<title>Timer </title> 
</head> 
<body>
<h1>Timers</h1>
<script>

if(!window.timers){
    var timers= [], i= 0;
    while(i<5){
        timers.push(setInterval(function(){
            if(confirm(timers.join('\n')+'\nRemove a timer?')){
                clearInterval(timers.shift());
            }
        },
        i*1000+1000));
        ++i;
    }
}
</script>

</body> 
</html> 
kennebec
A: 

Look at the scripts below, the browser could remember the id of each setTimeout iteration

for (i = 1; i <= d; i++) {
          (function(j) {
                var delay = j/d; 
               t[j] = setTimeout(function() {      
                      elem.style.top = j+"px";
                     },delay);

            })(i);           
 } 

You can access them by

for (i in t) {
      alert(t[i]);  
 }
unigg
+1  A: 

It is up for the developer to keep track of. You can do so by using the returned value of the setTimeout/setInterval function and passing that value to the clearTimeout/clearInterval function - as described in other answers here.

This appears to be because each browser will implement keeping track of the intervals in their own way.

From w3.org/TR/2009/WD-html5-20090212/no.html (a draft, but w3schools and http://w3.org/TR/Window explain it almost the same way) - setTimeout and setInterval return a long and clearTimeout/clearInterval accept a long to find and cancel

s_hewitt