1

Possible Duplicate:
Viewing all the timouts/intervals in javascript?

I'm making an HTML5 game, and recently I've added a lot of graphical effects that rely on intervals (made by the setInterval function). It started running slow, even when no intervals should be running! So, I believe that I'm not clearing them all, and I'd like to do that. But how? First of all, of course, is to figure out if that's even the problem, so how do I get the number of currently active intervals?

Damn, the interval count never goes up more than 3, I was careful after all. I think I just set the terminal velocity too low!

Community
  • 1
  • 1
corazza
  • 31,222
  • 37
  • 115
  • 186
  • 2
    *"...how do I get the number of currently active intervals?"* Keep track of them. –  Apr 22 '12 at 15:47
  • 5
    Wrap the `setInterval`, `clearInterval` and their `Timeout` brethren in your own versions that do bookkeeping. – DCoder Apr 22 '12 at 15:47

1 Answers1

2

Wrap all Interval functions. For example:

myInterval = function(func, sec) {
   myInterval.count = myInterval.count + 1 || 1;
   setInterval(func, sec); 
};
myInterval("console.log(1)", 3000);
myInterval("console.log(2)", 3000);

Then

console.log(myInterval.count); // => 2

The same for clearInterval, and Timeout functions

Mike
  • 1,042
  • 1
  • 8
  • 14