I’m writing a closure that looks like this:
(function(){
var OutterScope = 1;
(function RunThisLoop() {
OutterScope++;
console.log(OutterScope);
setInterval(RunThisLoop, 1000);
})();
})();
Here is the jsfiddle. The problem is that the console output seems off: it’s supposed to update every second but it seems to be updating by more than one second every second and then it eventually crashes the browser. Why is that?
Likely its running out of resources from invoking
RunThisLoopand setting up the interval again and again and again…You might want
setTimeoutinstead or move thesetIntervaloutside ofRunThisLoop.