views:

30

answers:

3

I'm working on an ajax web appliation which contains many running timeouts and intervals. And now I need to clear all running timeouts and intervals sometimes. Is there a simple way to stop everything without need to store every timeout and interval ID and iterate through them and clear them?

+3  A: 

Short answer: NO

Longer answer: This should show some means to get at the intervals but it does not

<div id="msg"></div>
<script>
function test() {
  document.getElementById('msg').innerHTML=x+':'+new Date();
  cnt++;
  if (cnt==3) { alert('hey'); clearAll(); }
}
var x = setInterval('test()',1000)
var y = setInterval('test()',2000)
var cnt = 0;
var win = window;
for (var obj in window) {
  if (obj==x) alert('found!')
  document.write('<hr>'+obj+':'+typeof obj)
}  
alert(window.x+'-'+window.y)
function clearAll() {
  clearInterval(); // does not work
}

</script>
mplungjan
Tried to find window objects that had a typeof number - funnily enough IE assigns an 8 digit number, FF a single digit starting with 2
mplungjan
A: 

clearTimeout() - cancels the setTimeout() Look at this http://www.w3schools.com/js/js_timing.asp

Amit
Yes but notvar x = setInterval('something()',2000);you need clearInterval(x)ditto for setTimeout.
mplungjan
okkk thanks for pointing out...
Amit
+1  A: 

You cannot clear any timeouts and intervals you don't know about.

You'd need something like getTimeoutList which isn't in the DOM3 spec, or even planned, AFAIK.

Brock Adams