admin管理员组文章数量:1290188
Specifically talking about (server side) V8, and assuming I'm not concerned about accuracy because I can detect and compensate for it, could I literally set up thousands of relatively simple timeouts several seconds apart from each other using setTimeout without facing any other limit other than RAM? Is there any catch I should be aware of if I were to use a system where there may be thousands of scheduled timeouts at any given time?
For the record I've read John's Resig excellent article on How Javascript Timers work so no need to point out anything that's already covered there :) I'm aware node.js is single threaded, timers can block other timers if they take too long, etc.
PS: I'm strictly trying to understand how viable is what I describe, no need to point out "there's surely a better way to do what you intend to do!".
Specifically talking about (server side) V8, and assuming I'm not concerned about accuracy because I can detect and compensate for it, could I literally set up thousands of relatively simple timeouts several seconds apart from each other using setTimeout without facing any other limit other than RAM? Is there any catch I should be aware of if I were to use a system where there may be thousands of scheduled timeouts at any given time?
For the record I've read John's Resig excellent article on How Javascript Timers work so no need to point out anything that's already covered there :) I'm aware node.js is single threaded, timers can block other timers if they take too long, etc.
PS: I'm strictly trying to understand how viable is what I describe, no need to point out "there's surely a better way to do what you intend to do!".
Share Improve this question edited Aug 29, 2012 at 0:27 Mahn asked Aug 28, 2012 at 22:57 MahnMahn 16.6k17 gold badges65 silver badges80 bronze badges 3 |3 Answers
Reset to default 16The only real world limit you may come up against is the amount of memory available to node. Use the following code to test. I successfully ran the example below using oneMillion and int32Max. When using int64Max, I received the following error from node. I'm using 64bit windows with 4gb of RAM.
FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory
Node code to test:
var util = require('util');
var int64Max = 9007199254740992;
var int32Max = 2147483647;
var oneMillion = 1000000;
var tenThousand = 10000;
var counter = 0;
//Exchange the limiter with one of the above vars to test.
for (var i = 0; i < oneMillion; i++){
setTimeout(log, 1);
//Required as the timeout/callback method will not be called until the loop ends due
//to node/js being single threaded.
util.log('loop:' + i);
}
function log(){
util.log('callback: ' + counter++);
}
I don't know how node operates, but if you create MAXINT+1 timers without letting them run, you risk an integer overflow.
I have come to find, through my own experience, that a single setTimeout
has a maximum delay of 2500000000 milliseconds (about 29 days). Anything beyond that delay, and it actions the code/function straight away - as if the delay were 0 milliseconds.
本文标签: javascriptIs there any limit to setTimeoutStack Overflow
版权声明:本文标题:javascript - Is there any limit to setTimeout? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738435588a2086656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
for
loop away. Try it and tell us instead. ;) – Jon Commented Aug 28, 2012 at 23:40