Javascript 投票服务器。 这会导致堆栈溢出吗?
我不太熟悉每个浏览器上每个 javascript 实现的细节。 但我确实知道使用 setTimeout 时,传入的方法会在单独的线程上调用。 那么,在方法内部递归地使用 setTimeout 会导致其堆栈无限增长,直到导致堆栈溢出吗? 或者它会创建一个单独的调用堆栈并在当前帧失去焦点时销毁它? 这是我想知道的代码。
function pollServer()
{
$.getJSON("poll.php", {}, function(data){
window.setTimeout(pollServer, 1000);
});
}
window.setTimeout(pollServer, 0);
我想每隔一秒左右轮询一次服务器,但不想通过“阻塞循环”浪费 CPU 周期 - 而且我不想在浏览器死机之前设置用户可以访问页面的时间限制。
编辑
使用 firebug,我设置了一些断点,并通过查看“Script -> Stack”面板看到调用堆栈实际上只是“pollServer”,并且每次调用都不会增长。 这很好——但是,JS 的其他实现是否有不同的行为?
I am not too familiar with the specifics of every javascript implementation on each browser. I do know however that using setTimeout, the method passed in gets called on a separate thread. So would using a setTimeout recursively inside of a method cause its stack to grow indefinitely until it causes a Stack Overflow? Or would it create a separate callstack and destroy the current frame once it goes out of focus? Here is the code that I'm wondering about.
function pollServer()
{
$.getJSON("poll.php", {}, function(data){
window.setTimeout(pollServer, 1000);
});
}
window.setTimeout(pollServer, 0);
I want to poll the server every second or so, but do not want to waste CPU cycles with a 'blocking loop' - also I do not want to set a timelimit on how long a user can access a page either before their browser dies.
EDIT
Using firebug, I set a few breakpoints and by viewing the "Script -> Stack" panel saw that the call stack is literally just "pollServer" and it doesn't grow per call. This is good - however, do any other implementations of JS act differently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定它是否会造成堆栈溢出,但如果周期恒定,我建议您使用
setInterval
。这就是 prototype 实现其
PeriodicalExecuter
的方式。I am not sure if it would create a stack overflow, but I suggest you use
setInterval
if the period is constant.This is how prototype implements its
PeriodicalExecuter
.setTimeout 在事件泵循环中稍后的某个时间执行。 传递给 setTimeout 的函数不是延续。
如果您停下来想一想,超时函数共享调用堆栈有什么有用的目的或证据。
一旦你回答了这些问题,很明显答案是不。
setTimeout executes sometime later in the future in the event pump loop. Functions passed to setTimeout are not continuations.
If you stop and think about it, what useful purpose or evidencec is there that the call stack is shared by the timeout function.
Once you answer those questions it clearly becomes evident the answerr is NO.
setTimeout 不会增加调用堆栈,因为它会立即返回。 至于你的代码是否会在任何浏览器中无限期地运行,我不确定,但似乎有可能。
setTimeout does not grow the callstack, because it returns immediately. As for whether your code will run indefinitely in any browser, I'm not sure, but it seems likely.
看一下 jQuery“SmartUpdater”插件。
http://plugins.jquery.com/project/smartupdater
提供以下功能:
take a look at the jQuery "SmartUpdater" plugin.
http://plugins.jquery.com/project/smartupdater
Following features are available: