7

It seems if I'm scrolling the window, the window.setInterval doesn't get attached / fired while the scrolling is happening or after. Has anyone else seen the same issue?

I mean...

  • What could be causeing this?
  • What can I do to fix this?
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Johnny
  • 2,800
  • 6
  • 25
  • 33

4 Answers4

9

iOS halts almost everything in response to user touch to guarantee it feels responsive. The setInterval issue is known, and there doesn't appear to be a workaround.

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

EDIT

During the "freeze" the timer will not catch up once the user releases the screen. The missed events are not deferred, but lost entirely (a bug).

Community
  • 1
  • 1
jimp
  • 16,999
  • 3
  • 27
  • 36
  • it does not just pause it, it doesn't fire even after the scrolling. The pausing issue is different, it happens if the setInterval is attached before the page scroll. In my case, it's attached during page scroll. – Johnny Oct 02 '12 at 05:41
  • Right. During the "freeze" the timer will not catch up once the user releases the screen. But how does your event get attached *during* the scroll if the JS engine is paused during that time? Are you sure it's not just triggering during the scroll and getting discarded? – jimp Oct 02 '12 at 06:08
  • I have the same problem. I am getting touch events if the user scrolls,lets go and then touches again. But my setTimeout are discarded. My ugly hacky solution is do a cleanup after each scroll event. (super ugly!) – Darwin Nov 05 '12 at 21:58
  • This answer isn't correct. The timeout *is never fired*; it's silently discarded. It's a horrible, evil bug. – Glenn Maynard Feb 10 '13 at 19:27
  • @GlennMaynard Why is my answer incorrect? The OP asked if its known behavior (yes) and if there's a solution (no, at least not from Apple). Whether it's a bug or Apple just doesn't care, I'm not sure why it deserved a downvote. – jimp Feb 10 '13 at 21:36
  • This talks about timers being deferred during scrolling, but the OP asked about setTimeout never being fired at all, which is a different beast entirely. – Glenn Maynard Feb 10 '13 at 22:31
  • @GlennMaynard Oh, I see. My first comment to Johnny clarified my intent to not simply suggest deferment, which is why he accepted it as the right answer. I should have edited my answer instead (and I just did). – jimp Feb 11 '13 at 04:15
3

Found this (scary but amazing) workaround, and it's working for me in iOS 6.0:

https://gist.github.com/3755461

0

I'm not completely sure, but you could use a setTimeout instead of setInterval? It's generally bad practice to use setInterval anyway.

var delay = 100;
(function callee() {
    setTimeout(callee, delay);
})();
Nate Higgins
  • 2,104
  • 16
  • 21
  • I'm not sure if I agree that using `setInterval` is _generally_ a bad practice (it can definitely be overused though, especially for redrawing where requestAnimationFrame is better suited). However, an argument can be made for `arguments.callee` being a bad practice (which is why it's been [removed from strict mode](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/Strict_mode?redirectlocale=en-US&redirectslug=JavaScript/Strict_mode)). – Strille Oct 02 '12 at 06:59
  • 2
    Issue persists with setTimeout – TaylorMac Apr 30 '14 at 20:54
0

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

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

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

Mithun Sreedharan
  • 49,883
  • 70
  • 181
  • 236