How can I force Jquery animations to occur when the window is not focused

泪湿孤枕 提交于 2019-12-04 19:47:47

Depending on the animations, you could add onfocus event on the body element that gets the animation queue by calling .queue() and if it's longer than one element, you could call .clearQueue(). Of course you'd have to be careful not to mess up the layout, since it removes all the animations.

Also you could try doing jQuery.fx.off = true if the list is longer than one element. This should set the duration of all animations to 0, which should result in all of the queued animations being finished instantly, and to enable animations once again, set the value to false. However, since I haven't tried it myself, I'm not sure if it sets the duration only on the animations that haven't yet been run (already in a queue) or on only those that haven't yet been added to a queue. If the latter case is true, you could loop through the queue and set each duration to 0 manually.

Similar to Zanfa's answer - you could use the focus event, but you could also use it in combination with the blur event.

So rather than trying to clean everything up on focus, you could use the blur event to cleanly pause the game, stop any animations and then later use the focus to get things going again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!