通过使用javascript递归setTimeout函数,获取stackoverflow是否有风险? [英] By using javascript recursive setTimeout function, is it risky to get the stackoverflow?

查看:131
本文介绍了通过使用javascript递归setTimeout函数,获取stackoverflow是否有风险?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用javascript递归setTimeout函数,获取stackoverflow是否有风险?

By using javascript recursive setTimeout function, is it risky to get the stackoverflow?

通过尝试此示例,您可以在浏览器控制台中看到堆栈增长。为什么会这样?

By trying this example you can see in browser console that the stack grows. Why is it so?

var iteration = 0;
  function bar() {
    iteration++;
    console.log("iteration: " + iteration);
    console.trace();
    if(iteration < 5){
    	setTimeout(bar, 5000);
    } 
  }

bar();

推荐答案


通过使用javascript recursive setTimeout函数,获取stackoverflow是否有风险?

By using javascript recursive setTimeout function, is it risky to get the stackoverflow?

否。 setTimeout 注册一个处理程序,当计时器触发时,该处理程序将被浏览器调用。到发生这种情况时,堆栈已经解开(如果没有,则计划超时的任务不会结束,并且浏览器的UI将被锁定等待它结束)。

No. setTimeout registers a handler that will get called by the browser when the timer triggers. By the time that happens, the stack has unwound (if it hadn't, the task that scheduled the timeout wouldn't have ended, and the browser's UI would be locked up waiting for it to end).


通过尝试此示例,您可以在浏览器控制台中看到堆栈增长。

By trying this example you can see in browser console that the stack grows.

不,在下一次调用处理程序之前,堆栈会展开。如果您指的是Chrome的devtools向您展示的异步堆栈条目,那些不是真正的堆栈,它们是devtools工件。 (启动计时器,注意两个刻度,以便在第二个上看到异步条目,然后关闭控制台,再等两个刻度,然后重新打开它;注意没有任何异步条目&nd;—在关闭控制台之前,甚至连你看到的那个都没记录!)

No, the stack unwinds before the handler is next called. If you're referring to the async "stack" entries that Chrome's devtools show you, those aren't the real stack, and they're a devtools artifact. (Start your timer, watch for two ticks so you see the "async" entry on the second one, then close your console, wait two more ticks, and reopen it; notice that there aren't any "async" entries — at all, not even the one you saw it log before closing the console!)

这篇关于通过使用javascript递归setTimeout函数,获取stackoverflow是否有风险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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