0

I have seen some JavaScript code that has the following:

var myFunc = function () { 
    timeout = setTimeout(myFunc, 0); 
}

It seems that this would immediately recall the function.

What use case is there for this?

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • Also see http://stackoverflow.com/a/4575011/453277 (which I think explains it better). – Tim M. Aug 21 '13 at 20:46
  • how is this a duplicate if the other questions were about setTimeout, which is NOT the same as setInterval? i've not thought of using setInterval(f,0), so it seems like a new concept to me... – dandavis Aug 21 '13 at 21:19

1 Answers1

1

Read this.

In short, it "pauses" the JavaScript execution to let the rendering threads catch up. It gives the browser a chance to finish doing some none-JavaScript things that have been waiting to finish before attending to this new piece of JavaScript.

Community
  • 1
  • 1
federico-t
  • 12,014
  • 19
  • 67
  • 111