admin管理员组文章数量:1332395
I have a pretty normal setInterval
call (I think):
var myInterval = setInterval("myFunction('passVar1', 'passedVar2')", 8000);
It runs perfectly if I'm on the page, but if I leave the page open for awhile (or minimize it?), when I e back, it's like it just built up all the myFunction calls into a queue and tries to run through them all really fast to "catch up". Then, when it catches back up, it runs at 8 seconds apart again like it's supposed to.
Am I doing something wrong? I'm using Firefox 5 at the moment - if that has anything to do with it. Thoughts? TIA.
I have a pretty normal setInterval
call (I think):
var myInterval = setInterval("myFunction('passVar1', 'passedVar2')", 8000);
It runs perfectly if I'm on the page, but if I leave the page open for awhile (or minimize it?), when I e back, it's like it just built up all the myFunction calls into a queue and tries to run through them all really fast to "catch up". Then, when it catches back up, it runs at 8 seconds apart again like it's supposed to.
Am I doing something wrong? I'm using Firefox 5 at the moment - if that has anything to do with it. Thoughts? TIA.
Share Improve this question asked Jun 29, 2011 at 15:32 DaveDave 29.1k26 gold badges115 silver badges184 bronze badges 3- 2 Modern browsers do that on purpose. I think the idea is to reduce the system load imposed by non-focused tabs/windows, and then, when the window regains focus, they allow slowed interval timers to catch up. – Pointy Commented Jun 29, 2011 at 15:34
- @Pointy - that sucks. Any way around it? It looks very bad when the user es back to my page, and it looks like it's tweaking out. Clicking a button on the page to clear the interval doesn't even work - it still has to catch up before it clears it. – Dave Commented Jun 29, 2011 at 15:38
- 2 I agree, it causes all sorts of goofy effects. The only thing I can imagine would be to have the interval-driven code explicitly check to see if it's running on schedule (by checking actual elapsed time). It might even be easier/better to avoid interval timers altogether and do everything with "setTimeout()"; some people prefer that anyway as it's easier to control. – Pointy Commented Jun 29, 2011 at 15:47
2 Answers
Reset to default 7For one thing fix your setInterval
so it doesnt use eval:
var myInterval = setInterval(function(){
myFunction('passVar1', 'passedVar2')
}, 8000);
Second, it depends on the browser what the interval does or does not do.
UPDATE:
Based on ments below and the related chat discussion here is a fiddle using setTimeout
:
http://jsfiddle/maniator/VR7WE/
UPDATE:
Based on further discussion, turns out it was an issue with jquery's animate
The following fiddle only runs the timeout when the animate is done: http://jsfiddle/maniator/c869Z/5/
You can drive your own interval mechanism with "setTimeout()", and that will allow your code to adapt to the actual browser behavior. You'd probably do something to keep track of "target" times, and when you miss the target the next timeout would be correspondingly smaller than the nominal delay. That way, even if your nominal delay was 100ms and the browser were only giving you control every 1000ms, you'd only have one pending timeout at any time. When focus returns, your code would then notice things getting back to normal and resume the higher-frequency activity pretty much automatically.
本文标签: Javascript setInterval quotcatches upquot after page is minimizedawayStack Overflow
版权声明:本文标题:Javascript setInterval "catches up" after page is minimizedaway - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742282506a2446352.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论