Krb686 / nanotimer

A much higher accuracy timer object that makes use of the node.js hrtime function call.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

clearInterval causes RangeError: Maximum call stack size exceeded

rsmck opened this issue · comments

Other than this warning it's working as expected, but if I remove the clearInterval then the error is resolved, but the timer never stops (which is obviously a problem);

Relevant code;

It's started here;

   playbackTimer = new NanoTimer();
   timerThreads[threadId] = playbackTimer;

   playbackTimer.setInterval(fdLevel,[fsteps,threadId],tinterval+'m', function() {
      playbackTimer.clearInterval();
   });

And the fdLevel function, which is called with the interval is as follows;

function fdLevel(maxsteps, threadId) {
   /* SOME CODE HERE OMITTED */

   if (threadCounter[threadId] == maxsteps || bolStopTimer == true) {
      // end cleanly
      timerThreads[threadId].clearInterval();
      delete threadCounter[threadId];
   }
}

timerThreads is defined as an object and uses a unique ID to refer to each timer object, the 'threadId' is a GUID that is passed to the function so it can stop itself (and so other, external, functions can potentially stop running timers)

Not too sure what I'm doing wrong here - it worked fine with the old setInterval (although the timing was horrifically inaccurate, hence why I'm using nanotimer :))

Actually, just noticed that the reference above to playbackTimer is different - this was my (failed) attempt at eliminating it being stored in an array being the problem, the original code (which behaves the same) is this;

   timerThreads[threadId] = new NanoTimer();

   timerThreads[threadId].setInterval(fdLevel,[fsteps,threadId],tinterval+'m', function() {
      timerThreads[threadId].clearInterval();
   });

Okay I have tested your code and here is what I have found out.

Your problem is being caused because you are calling .clearInterval() in two places.

You first call .clearInterval() inside function fdLevel. So the timer goes to clear the interval, and it sees there was a callback specified, so it calls it. That callback runs (the one you passed in), and you call .clearnterval() again, where it sees a callback was specified, etc, so it loops until the stack size is exceeded.

All you need to do is remove .clearInterval() from inside your callback. The callback should only contain code that needs to run once the chain of intervals has stopped. So you wouldn't need to attempt to stop it there again and if you don't have anything that needs to happen or anything that needs to be notified of when the intervals have stopped, then you may not need a callback there at all.

I know it's sort of confusing. It's not really the callback for when your task was run, it's just the callback for the chain of setIntervals to notify when they have been stopped, in the event you do clear them.

I'll be closing this. If you have any further questions don't hesitate to ask! Or any other further issues. And apologies for the delayed response.