setInterval doesn't work on iPhone
It's reported that a setInterval gets paused when the soft keyboard is open. (Same bug on Android)
http://code.google.com/p/android/issues/detail?id=13540 http://www.barneyb.com/barneyblog/2011/02/20/settimeout-bug-in-android-2-2/
Any work-arounds? I'm working on a website with content refreshing every x-seconds with set开发者_开发知识库Interval using Ajax Load. It works on every PC browser, but not on iPhone/Android.
I know this isn't an ideal solution but you could use a HTML5 worker instead of setInterval (though I don't know if they are paused in the background as well - I'm assuming they are not, that would be very odd if they were)?
And it does add lots of backwards compatibility issues for PC browsers that are not HTML5 compliant :(
http://www.sitepoint.com/javascript-threading-html5-web-workers/
A possible workaround would be to use an interval that is shorter than your desired refresh of X seconds, but it only actually fires the callback when the appropriate time has elapsed. Certainly more of a hassle, but not all that complicated either.
EDIT: Some sample code
var setiPhoneInterval = function(callback, delay){
var checkInterval = delay / 10; // break down original delay into 10 sub-intervals for checking
var lastFired = new Date();
var fire = function(){
var now = new Date();
if(now.getTime() - lastFired.getTime() > delay ){
lastFired = new Date();
callback();
}
};
var interval = setInterval(fire, checkInterval);
return interval;
};
var myCallback = function(){
console.log('fired!');
};
var foo = setiPhoneInterval(myCallback, 10000);
// and, you can still clearInterval if you'd like
// clearInterval(foo);
精彩评论