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

后端 未结 13 1953
被撕碎了的回忆
被撕碎了的回忆 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:38

    For me it's not important to play audio in the background like for others here, my problem was that I had some animations and they acted like crazy when you were in other tabs and coming back to them. My solution was putting these animations inside if that is preventing inactive tab:

    if (!document.hidden){ //your animation code here }
    

    thanks to that my animation was running only if tab was active. I hope this will help someone with my case.

提交回复
热议问题