1

If I call the update function several times I get multiple instances of this interval running. How can I be sure that this only gets triggered once per idVar?

For example if I run this twice in a row with the same idVar I get two updates per second, where it should only be one.

If the idVar is different it should run multiple times. idVar is used in Update().

intervals[idVar] = setInterval(function() {
    update();                            
}, 1000);
  • 2
    Just use a condition, `if ( ! idVar in intervals ) ...` – adeneo Nov 16 '16 at 19:22
  • 6
    Use `setTmeout()` instead of `setInterval()`. – Scott Marcus Nov 16 '16 at 19:23
  • @ScottMarcus yeah, I wonder why interval instead of timeout... – DontVoteMeDown Nov 16 '16 at 19:23
  • This question might help you choose another option as well: http://stackoverflow.com/questions/9516900/how-can-i-create-an-asynchronous-function-in-javascript – AJ X. Nov 16 '16 at 19:28
  • `setTimeout` does only get called once, BUT if called _**multiple**_ times and set to the _**same**_ object property, **multiple** instances will still be created. **The original question asks how to avoid multiple instances of intervals/timeouts being instantiated**. I've made a submission to edit the question to clarify this as well as adding my answer below – Pineda Nov 16 '16 at 19:58
  • 1
    Can you post your **update()** function, please? – Pineda Nov 16 '16 at 20:14
  • all of these comments miss the point.. I'm guessing the easiest way is to have an outside variable that acts as a semaphore - and a try { } finally { } sort of scenario ... – João Antunes Aug 09 '19 at 14:24

4 Answers4

1
intervals[idVar] = setTimeout(function() {
    update();                            
}, 1000);
Dan Wilson
  • 3,937
  • 2
  • 17
  • 27
1

If you really need to use setInterval and dont want to use setTimeout , you can clear it when you enter function

For example

var interval

interval = setInterval(function(){
   clearInterval(interval)
  //Your code
});
noitse
  • 1,045
  • 9
  • 27
0

If you really want a new instance of the interval running for each unique idVar, you just need to check to see if you already have an instance of the interval running for the current idVar:

if (!intervals[idVar]) {
    intervals[idVar] = setInterval(function() {
        update();                            
    }, 1000);
}
Sean Cogan
  • 2,516
  • 2
  • 27
  • 42
0

setInterval returns a timeoutID that is used to identify it.

To stop a setInterval you pass it's timoueID to clearInterval.


Explanation of why you get two intervals running

When you do this:

intervals[idVar] = setInterval(function() { update(); }, 1000);
intervals[idVar] = setInterval(function() { update(); }, 1000);

What you are doing is:

  • call your first setInterval and setting intervals[idVar] to this first setInterval's timeoutID

  • call your second setInterval and overwrite the value stored in intervals[idVar] with the timeoutID of your newly called setInterval

Result: Two seperate intervals, with intervals[idVar] storing the timeoutID of the second one called


setTimeout

setTimeout is like setInterval and actually uses the same pool of timeout IDs, but it is only invoked once after the specified delay.


Setting one interval (setTimeout or setInterval) per idVar

var idVar = setInterval(function() { 
                          update(); 
                        }, 1000);  // sets idVar to the timeoutID and starts the interval
}
intervals[idVar] = idVar;  // assigns a property 'idVar' on `intervals`
                           // and sets the timeoutID as its value
                           // allows you to call clearInterval(intervals[idVar]);

However, without knowing exactly what your update() method is doing it is difficult to tell what the best solution would be here...

Pineda
  • 7,435
  • 3
  • 30
  • 45