I found this snippet of code on a website that discusses async callback functions and the event loop in JavaScript. I am curious as to why the line
timer = setTimeout(arguments.callee, 0)
does not create a recursive loop since it is being executed without any delay. According to logic, settimeout should be invoked repeatedly, preventing the following IF Statement from executing. However, in actuality, the if statement does get executed. Why?
var i = 0, diff = 0, d = new Date()
var timer = setTimeout(function() {
diff += new Date() - d
timer = setTimeout(arguments.callee, 0)
if (i++==1000) {
clearTimeout(timer)
alert("Resolution: "+diff/i)
}
d = new Date()
}, 0)