2

I saw an answer about the correct usage of setInterval() function. It was said that

(function(){
   // do some stuff
   setTimeout(arguments.callee, 60000);
})();

guarantees the next call from setTimeout isn't made before the previous has ended. Why using self-invoking functions makes this happen?

Bruno Henrique
  • 757
  • 10
  • 21
  • 2
    This "correct usage" is actually incorrect, since `arguments.callee` is deprecated and forbidden in strict mode, for various reasons. Instead, use named functions. See http://stackoverflow.com/questions/103598/why-was-the-arguments-callee-caller-property-deprecated-in-javascript/235760 – tcooc Oct 14 '15 at 18:35

1 Answers1

5

Using an immediately-invoked function expression doesn't make it happen, it's using setTimeout() instead of setInterval() that does it. The mechanism of setInterval() will not really start the next iteration before the first one has finished, unless the handler does something like begin an asynchronous operation. But by using setTimeout instead, it's possible to have more control over the delays between iterations.

Also it really shouldn't be written that way. Instead:

(function f() {
  // code
  setTimeout(f, 60000);
})();

Using arguments.callee is a bad idea when it's not strictly necessary.

Pointy
  • 405,095
  • 59
  • 585
  • 614