Does calling setTimeout clear the callstack?

后端 未结 3 1025
遥遥无期
遥遥无期 2020-12-03 17:31

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 sho

3条回答
  •  不知归路
    2020-12-03 18:10

    I can confirm that the stack is cleared.

    Consider this scenario:

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

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

    Stack at first breakpoint:

    • c()
    • b()
    • a()

    Stack at second breakpoint:

    • d()

    Live demo: http://jsfiddle.net/nbf4n/1/

提交回复
热议问题