user3129452 user3129452 - 3 years ago 88
Javascript Question

javascript Call function 10 times with 1 second between

How to call a function 10 times like

for(x=0; x<10; x++) callfunction();


but with 1 sec between each call?

Answer Source

You can use setInterval for repeated execution with intervals and then clearInterval after 10 invocations:

callfunction();
var callCount = 1;
var repeater = setInterval(function () {
  if (callCount < 10) {
    callfunction();
    callCount += 1;
  } else {
    clearInterval(repeater);
  }
}, 1000);

Added: But if you don't know how long it takes your callfunction to execute and the accurate timings between invocation starting points are not important it seems it's better to use setTimeout for reasons mentioned by Paul S and those described in this article.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download