How can I make setInterval also work when a tab is inactive in Chrome?

后端 未结 13 2164
被撕碎了的回忆
被撕碎了的回忆 2020-11-21 09:59

I have a setInterval running a piece of code 30 times a second. This works great, however when I select another tab (so that the tab with my code becomes inacti

13条回答
  •  没有蜡笔的小新
    2020-11-21 10:30

    I think that a best understanding about this problem is in this example: http://jsfiddle.net/TAHDb/

    I am doing a simple thing here:

    Have a interval of 1 sec and each time hide the first span and move it to last, and show the 2nd span.

    If you stay on page it works as it is supposed. But if you hide the tab for some seconds, when you get back you will see a weired thing.

    Its like all events that didn't ucur during the time you were inactive now will ocur all in 1 time. so for some few seconds you will get like X events. they are so quick that its possible to see all 6 spans at once.

    So it seams chrome only delays the events, so when you get back all events will occur but all at once...

    A pratical application were this ocur iss for a simple slideshow. Imagine the numbers being Images, and if user stay with tab hidden when he came back he will see all imgs floating, Totally mesed.

    To fix this use the stop(true,true) like pimvdb told. THis will clear the event queue.

提交回复
热议问题