1

I would like to periodically call a function (assume it's name is "alter(id)") with the parameter changing every call. The parameter should increment with every call and return to 0 at some point. On a timeline it would look like this:

Time: function to call
0ms: alter(0)
300ms: alter(1)
600ms: alter(2)
900ms: alter(3)
1200ms: alter(0)
1500ms: alter(1)
and so on ad infinitum.
blues
  • 4,547
  • 3
  • 23
  • 39
  • @Chris: I am aware of this question here: http://stackoverflow.com/questions/457826/pass-parameters-in-setinterval-function but don't know how to adapt it to a case where the parameter should change every call. – blues Sep 20 '12 at 14:42

1 Answers1

0
    let counter = 0; // closure
    const INTERVAL = 300;// 300ms
    const alter = (arg) => { /* alter function body */ }
    const tick = () => {
      counter = alter(counter);
      counter = a < 3 ? a + 1: 0;
    }   
    tick(); // 0ms
    setInterval(tick, INTERVAL); // call each 300 ms

setInterval documentation

closure

Nikolai Borisik
  • 1,501
  • 13
  • 22