-1

So I know that you can assign setInterval to a variable, and later pass that variable to clearInterval to disable the timer.

What I'm wondering:

Say I have a global variable that is null on page initiation. When a specific function gets called then the global variable is assigned setInterval(fname, seconds). When I don't want this interval running anymore, I set the global variable to null.

For what I'm specifically testing (a sequence of images being rendered one after the other) it seems to work, but I wonder if I'm making some sneaky error.

Here's an overview of my code:

MyStuff.controls = {};
MyStuff.controls.animation_timer = null;

MyStuff.controls.start = function () {
    if (!MyStuff.controls.animation_timer) {
         MyStuff.controls.animation_timer = setInterval(MyStuff.controls.animate, 500);
    }
}

MyStuff.controls.stop = function () {
    MyStuff.controls.animation_timer = null;
}
pickle
  • 855
  • 1
  • 7
  • 16
  • 1
    I don't see where you call `clearInterval`. – Dave Newton Jun 25 '15 at 20:53
  • No I don't ever call it. I'm wonder if setting the variable to null is equivalent to clearInterval. – pickle Jun 25 '15 at 20:55
  • @DaveNewton I think that's the point... – Mikk3lRo Jun 25 '15 at 20:55
  • 2
    no, you should stop the interval instead of doing nothing all the time. – dandavis Jun 25 '15 at 20:56
  • I'm just curious what happens. If I set it to null, is the interval still running but just with a function that doesn't do anything? – pickle Jun 25 '15 at 20:57
  • @val: No, `MyStuff.controls.animate` is still doing what it is always doing. – Bergi Jun 25 '15 at 20:57
  • What are you testing this in (where does it work / stop doing stuff)? – Mikk3lRo Jun 25 '15 at 20:58
  • I don't understand the question. If you know what the variable is so that you can assign `null`, why not do the obvious thing and call `clearInterval()`? This seems like a 'solution' looking for a problem. –  Jun 25 '15 at 20:59
  • I'm not sure that it is still doing what it's always doing. The 'MyStuff.controls.animate' stops what it was doing when I set 'MyStuff.controls.animation_timer' to null. As in, it stops loading the next image in the sequence and it doesn't draw it. This is in javascript with a framework called OpenLayers – pickle Jun 25 '15 at 21:01
  • @Hobo Sapiens: It's more so that I'm curious if this is an equivalent solution. – pickle Jun 25 '15 at 21:01
  • I don't see how setting it to null could stop the animation (and it doesn't for me). The return value of `setInterval` is an integer. Setting the value to null of something that held an int can't affect the original timer. – Dave Newton Jun 25 '15 at 21:06
  • Unrelated, but that's not a "global variable" in any traditional sense; it's namespaced. – Dave Newton Jun 25 '15 at 21:07
  • Oh my gosh, I'm so sorry. I feel like an idiot - I had been calling clearInterval. I wrote a lot of this code months ago and I just didn't see a clearInterval anywhere. So sorry - thank you all for your time anyways. My apologies. – pickle Jun 25 '15 at 21:27

3 Answers3

0

When I don't want this interval running anymore, I set the global variable to null.

No, that does not work. You can simply try out your code. It will continue executing your callback, even if you've thrown away the identifier that could have been used to stop the interval.

Just call clearInterval:

MyStuff.controls.stop = function () {
    clearInterval(MyStuff.controls.animation_timer);
    MyStuff.controls.animation_timer = null;
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Does your example work? Sure, I don't see why not.

Is it good form? Probably not. You're still going to be calling MyStuff.controls.animate on a regular basis but it'll simply do nothing (assuming it's watching and acts conditionally to the global variable). That's wasted processing right there. Here's an example of what that would look like. Check your console for the output. You can see that the function still runs, it just behaves differently.

I can't think of a good reason to not use setInterval/clearInterval.

If you need to ensure that only one instance is running, simply treat the global variable as a mutex. For example:

  • When you want to start the interval, check if the global variable. If it's null, run setInterval and store the result in the global variable. If it's non-null, the interval is already running, so don't call setInterval.
  • When you want to stop the interval, use clearInterval on the global variable, then set it to null.
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
0

I made a mistake. I was calling clearInterval I just didn't see it for whatever reason - the example I presented won't work. Sorry everyone!

pickle
  • 855
  • 1
  • 7
  • 16