admin管理员组文章数量:1323732
I'm doing a lot of recursion in Javascript, and to keep the stack from overflowing, I've been using setTimeout. Here's a quick theoretical example:
go(){
setTimeout(function(){
x++;
go();
},1);
}
I've also got a function logging x
to the console every few seconds, but that isn't the concern. What I'm seeing is that no matter what value I put in for the timeout, for which I've used 1 in the example, the script can only run 1000 times per second. I'm doing recursion on the level of hundreds of millions, so this isn't fast enough. When I set the timeout value to 0, or .1, or 1/10, I still only get approximately 1000 times per second. I've tried using 32 and 64 bit browsers (Chrome and Firefox) to no avail.
How can I kick the speed up a notch? Also, I'm relatively new at all of this, so it'd be awesome if the solution was a simple one.
Oh, forgot to mention: if I remove the setTimeout altogether, I overflow the stack every time.
Thanks for the help!
I'm doing a lot of recursion in Javascript, and to keep the stack from overflowing, I've been using setTimeout. Here's a quick theoretical example:
go(){
setTimeout(function(){
x++;
go();
},1);
}
I've also got a function logging x
to the console every few seconds, but that isn't the concern. What I'm seeing is that no matter what value I put in for the timeout, for which I've used 1 in the example, the script can only run 1000 times per second. I'm doing recursion on the level of hundreds of millions, so this isn't fast enough. When I set the timeout value to 0, or .1, or 1/10, I still only get approximately 1000 times per second. I've tried using 32 and 64 bit browsers (Chrome and Firefox) to no avail.
How can I kick the speed up a notch? Also, I'm relatively new at all of this, so it'd be awesome if the solution was a simple one.
Oh, forgot to mention: if I remove the setTimeout altogether, I overflow the stack every time.
Thanks for the help!
Share Improve this question edited Mar 10, 2013 at 0:22 Ian Zane asked Mar 10, 2013 at 0:10 Ian ZaneIan Zane 2,3895 gold badges24 silver badges21 bronze badges 7- I wonder why you are doing such heavy operations on client browser . Its better if you can find a way to run this on your server. – Avinash Singh Commented Mar 10, 2013 at 0:13
- 3 I'm not sure I understand; if you're using setTimeout to keep the stack from overflowing, then you must not need recursion. So why do you need setTimeout? Going slower with recursion will only delay the overflow. – Dave Newton Commented Mar 10, 2013 at 0:14
- 2 What you are doing doesn't seem right – adarsh Commented Mar 10, 2013 at 0:20
- There's a minimum timeout for setTimeout, but if you're targeting newish browsers you can use window.postMessage to do the same thing as fast as possible. I think MDN has an example. (Can't post a real answer now, on mobile.) – Jeremy Banks Commented Mar 10, 2013 at 0:23
- It might also be possible to use a custom event to do this, including on older browsers, but I haven't tried. – Jeremy Banks Commented Mar 10, 2013 at 0:27
4 Answers
Reset to default 4Your solution doesn't lie in making your current code run, but to rethink the code.
I don't know how you are using recursion in your code, but clearly you are using it wrong.
For any reasonable use of recursion, you would be far from overflowing the stack. If you are making recursive calls to a level of hundreds of millions, that is at least a million times too much.
A mon approach when using recursion is to divide the work in half for each level. That way you can handle all the items that you can fit in memory without going deeper than about 30 levels.
I tried something like you did and found the solution! You don't need recursion and the function setTimeout
, but all what you need is to use the setInterval
function on the function you want by 1 interval repeatedly in for
loop. For example if the for loop repeats itself 10 times, then 10 timers will execute the same function every 4 ms. The code will be executed repeatedly more and more quickly.
Your code should look like this for example:
function onload() {
for (var i = 0; i < 10; i++)
setInterval(go, 1);
}
function go() {
x++;
}
JavaScript is single threaded, and setTimeout will put your operation at the end of the queue. Even if you reduce the delay, you still have to wait for the previous operations to plete before the one you added kicks in.
It's not possible to make setTimeout wait less than 4 milliseconds. That is how setTimeout is defined in the HTML standard (official spec here). More likely your problem is with how your code is structured. Show us the rest of your code, maybe we can help sort it out.
本文标签: javascriptUsing setTimeout with intervals less than 1Stack Overflow
版权声明:本文标题:javascript - Using setTimeout with intervals less than 1 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742121910a2421749.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论