javascript 滚动期间在 iphone/ipad(移动 Safari)中的 setInterval 暂停

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11177774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:17:19  来源:igfitidea点击:

setInterval pauses in iphone/ipad (mobile Safari) during scrolling

javascriptiphoneipadsetinterval

提问by user1478076

I use the setInterval function in a website, and it works fine in IE, Chrome, Firefox and Safari. When i try it on ipad/iphone (safari mobile) i get problem: if i scroll the screen, the setInterval function pauses and it resumes only when i stop scrolling!

我在网站中使用 setInterval 函数,它在 IE、Chrome、Firefox 和 Safari 中运行良好。当我在 ipad/iphone(safari mobile)上尝试时,我遇到问题:如果我滚动屏幕,setInterval 函数会暂停,只有当我停止滚动时它才会恢复!

is there a way to prevent the function from pausing? Thanks

有没有办法防止功能暂停?谢谢

采纳答案by raina77ow

I'm afraid no, there's no way to prevent such behaviour. There's a plenty of topics here in SO about this problem (more-o-less related), here's a particularly interesting one. Its summary is simple:

恐怕不,没有办法阻止这种行为。这里有很多关于这个问题的主题(更多相关),这里有一个特别有趣的主题。它的总结很简单:

iOS elastic scroll can't be tracked. There's no event listener for it + both timeout / interval doesn't execute during scroll. And because there's no requestAnimationFrame in iOS5, this seems impossible to solve.

无法跟踪 iOS 弹性滚动。它没有事件侦听器 + 滚动期间不执行超时/间隔。而且由于iOS5中没有requestAnimationFrame,这似乎无法解决。

回答by Mithun Sreedharan

iOS6 Safari suffers from a bug that kills timers that are created while a page is scrolling.

iOS6 Safari 存在一个错误,该错误会杀死在页面滚动时创建的计时器。

There is a fix to this problem provided by kTmnh by recreating timers after scrolling finishes

kTmnh 通过在滚动完成后重新创建计时器来解决此问题

https://gist.github.com/3798925.

https://gist.github.com/3798925

回答by ZoharAdar

Check @Pattishall answer on this subject iOS 6 js events function not called if has setTimeout in it

检查@Pattishall 对此主题的回答iOS 6 js events function notcall if has setTimeout in it