为什么通过的setTimeout称为funcion没有调用堆栈限制? [英] why the funcion called by setTimeout has no callstack limit?
问题描述
timer = window.setTimeout(function () {
//do something
window.setTimeout(arguments.callee, 1000);
}, 1000);
其结果是,这些codeS工作得很好。
the result is that these codes work well.
但为什么不会吧导致下面的错误?
but why doesn't it cause the error below?
最大调用堆栈大小超过了
Maximum Call Stack Size Exceeded
debuging时,发现变量范围不包括previous的执行范围setTimeout函数
when debuging it, find the variable scope don't include the scope of previous executed "setTimeout function"
谁能解释一下吗?
与文件preferably!谢谢
with documentation Preferably!thanks
推荐答案
的setTimeout
是异步的(它在执行回调之前返回),并且回调将在一个被执行新的空堆栈帧。这就是全部的目的。
setTimeout
is asynchronous (it returns before executing the callback), and the callback will be executed on a new, empty stack frame. That's the whole purpose.
它不是一个递归调用,为此范围(以非尾部调用的情况下一> - 优化功能)需要被保留。但是,这也意味着该功能变得阻塞,这是不是你想要的。
It is not a recursive call, for which scope (in case of a non-tail-call-optimized function) would need to be retained. But that would also mean that the function became blocking, which is not what you want.
这篇关于为什么通过的setTimeout称为funcion没有调用堆栈限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!