调用setTimeout是否清除了callstack? [英] Does calling setTimeout clear the callstack?

查看:328
本文介绍了调用setTimeout是否清除了callstack?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用setTimeout方法调用函数而不是直接调用函数,可以在javascript中避免堆栈溢出吗?我对setTimeout的理解是它应该启动一个新的callstack。当我查看chrome和IE的callstack时,似乎setTimeout调用正在等待函数调用返回。

Can a stack overflow be avoided in javascript by using the setTimeout method to call a function instead of calling it directly? My understanding of setTimeout is that it should start a new callstack. When i look in the callstack of both chrome and IE it seems that the setTimeout calls are waiting for the function call to return.

这只是调试器的属性还是我的理解是否有缺陷?

Is this just a property of the debugger or is my understanding flawed?

编辑

虽然下面提供的答案是正确的,我遇到的实际问题与我调用setTimeout(aFunction(),10)的事实有关,因为括号因此立即评估aFunction。 这个问题将我排除在外。

While the answers provided below are correct, the actual problem I was having was related to the fact that I was calling setTimeout(aFunction(), 10) which was evaluating aFunction immediately because of the brackets. This question sorted me out.

推荐答案

我可以确认堆栈已清除。

I can confirm that the stack is cleared.

考虑这种情况:

function a() {
     b();   
}

function b() {
     c();   
}

function c() {
    debugger;
    setTimeout( d, 1000 );
}

function d() {
    debugger;
}

a();

因此有两个断点 - 一个在函数开头 c ,一个在函数开头 d

So there are two breakpoints - one at the beginning of function c, and one at the beginning of function d.

第一个断点处的堆栈:


  • c()

  • b()

  • a( )

第二个断点处的堆栈:


  • d()

现场演示: http://jsfiddle.net/nbf4n/1/

这篇关于调用setTimeout是否清除了callstack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆