[英]strange behaviour in recursive function
Could someone explain why the second function doesn't bring us a stack overflow? 有人可以解释为什么第二个函数没有给我们带来堆栈溢出吗?
//stack overflow on call
function test1() {
test1();
}
//no stack overflow, nor beer
function test2() {
setTimeout(test2, -500); //back to the future
}
Because it's not recursive. 因为它不是递归的。 The
test2
function is able to return, and some time later another invocation is scheduled by setTimeout
via the anonymous function that was created. test2
函数能够返回,一段时间后, setTimeout
通过创建的匿名函数安排另一个调用。
Obviously, you can't go back in time. 显然,您无法回到过去。
setTimeout
has a minimum duration. setTimeout
具有最小持续时间。
FWIW, the anonymous function is unnecessary. FWIW,匿名功能是不必要的。 You could do
setTimeout(test2, -500)
. 您可以执行
setTimeout(test2, -500)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.