It is well know that Mobile Safari pauses Javascript execution on a webpage when
You can subscribe to the window's "pagehide" and "pageshow" events to detect imminent suspension and reactivation of Javascript.
The problem is, those events do not fire when tab-switching (1.) on an iPad Mobile Safari. On an iPhone Mobile Safari everything is fine, just as described above.
It's trivial to demonstrate:
<!DOCTYPE html>
<html>
<head>
<script>
window.addEventListener("pagehide", function(evt){
var logger = document.getElementById('log_id');
logger.innerText = logger.innerText + " pagehide fired!";
}, false);
</script>
</head>
<body>
<div id="log_id"></div>
</body>
</html>
It fires on iPads (iOS5 and iOS6 Preview3) only when doing app-switching (2.) and not on tab-switching (1.). All iPhones work fine..
Has anyone been able to detect an imminent tab-switching on the iPad browser?
The reactivation of Javascript when the tab becomes active again can be detected by a heart beat loop as described in this discussion of the same topic.
Try to check focus and blur on document.
Why you need Page Visibility API?
I agree with Pinal: Use focus/blur! But i suggest not on document, but rather on window. Just register a listener to them and do your stuff in there.
As http://caniuse.com/#feat=pagevisibility states, the feature you want to use is not well implemented. (Edit: Just tested it in a mini test-case - it works on iOS 5/6 - even though caniuse.com asserts different)
If you try to use a timer, you could try requestAnimationFrame as an alternative to setInterval.
Fixed by Apple in iOS7. (Just tried in the iPad Simulator)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With