1

I understand the differences between setTimeout and setInterval, as this explained it quite nicely.

This question is about best practice when using timers, specifically setTimeout vs setInterval.

If I need to call a countdown several times in a game fairly frequently (for example, a player moves to a new location, which triggers a new countdown which will encourage them to move in 5 seconds), but it's not necessarily uniform intervals (meaning, they should move to next location, but can hang out at current location and be penalized), is it better performance-wise, and clean code-wise to:

a) keep clearing and setting setTimeout() when the player decides to change location

if (player.newLocation !== player.oldLocation) { //player location has changed
        window.clearTimeout(myTimer);
        myTimer = setTimeout(function() { 
            do something
        },50);  
} 

b) keep setInterval running, but change a variable that acts as that countdown each time the player moves to a new location

var counter = 0;
if (player.newLocation !== player.oldLocation) {
    counter = 100;
}

myTimer = setInterval(function() { 
    if (counter > 0) {
          // do something...
          counter--;
    }
},50);  
Community
  • 1
  • 1
user3871
  • 12,432
  • 33
  • 128
  • 268
  • What do you mean by "*consistently call*" but "*not necessarily uniform intervals*"? – Bergi Nov 13 '14 at 19:03
  • @Bergi worded that wrong. I mean I will be needing a countdown many times... as each time the player moves to a new specified location (like, jumps to a new square on a board, it will begin a new countdown, or reset the countdown) – user3871 Nov 13 '14 at 19:05
  • OK, I see. But these countdowns are not running concurrently, do they? – Bergi Nov 13 '14 at 19:09
  • @Bergi No they don't need to and shouldn't. – user3871 Nov 13 '14 at 19:10
  • 1
    for the use you describe, performance is not even a tiny factor. if you had an idle mode, setTimeout allows you to temporarily increase the delay without re-subscribing, but perf is about the same all around. – dandavis Nov 13 '14 at 19:14

2 Answers2

2

is it better performance-wise

Not keeping timers around when they are not needed is definitely better. Regardless whether they're implented with setTimeout or setInterval.

and clean code-wise

That depends on how (often) "the player decides to change location", and how this is signaled to your code. It might make an actual difference in behaviour, and you should aim to keep your code correct - independent from the sequence and timing of inputs. Keep it simple, and choose what you need.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • The player will need to make a decision to move within 5 seconds... but they don't necessarily need to move (ie, if they don't move, they will lose points) - which is where the "not necessarily uniform intervals) – user3871 Nov 13 '14 at 19:09
  • 1
    Sounds like you want individually triggered (started & stopped) intervals then. – Bergi Nov 13 '14 at 19:10
0

I think it's probably fine to do either one unless you are concerned about performance. ie you are doing something processor intensive when the countdown is complete. In that case I would say it depends on how often players move. If they are twitchy (move a lot) then go ahead and use setInterval. If they only move occasionally then the setTimeout would probably be better.