2

Granted the following piece of code:

function updateOdometers(odometers) {
    setTimeout(function(){
        odometers[1].update(odometers[1].value + 10);
    }, 500);

}

setInterval(updateOdometers(odometers), 2000);

For whatever reason, this code updates the value of odometer only once, rather than every 2000ms with a delay inside. Googling/SO-ing around didn't get me much of a result. Any ideas?

favoretti
  • 29,299
  • 4
  • 48
  • 61
  • Why are you trying to do this? – Bergi Mar 03 '15 at 20:39
  • 2
    Duplicate of http://stackoverflow.com/questions/28837247/javascript-setinterval-only-running-once – Telokis Mar 03 '15 at 20:40
  • Blah, didn't find the original question you're referring to. – favoretti Mar 03 '15 at 20:55
  • @Bergi: this is a not very sane sanitized piece of code. I have a number of odometers I need to update. The update has to happen every 6 seconds and inner odometers need to update one by one. Makes sense? – favoretti Mar 03 '15 at 21:03

1 Answers1

7

This line :

setInterval(updateOdometers(odometers), 2000);

should be

setInterval(function () {updateOdometers(odometers);}, 2000);

Otherwise you will be calling updateOdometers(odometers) and passing its result to setInterval.

Telokis
  • 3,399
  • 14
  • 36