简体   繁体   English

setInterval在iPhone上不起作用

[英]setInterval doesn't work on iPhone

It's reported that a setInterval gets paused when the soft keyboard is open. 据报道,打开软键盘时setInterval会暂停。 (Same bug on Android) (与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/ 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 setInterval using Ajax Load. 我正在一个网站上使用Ajax Load使用setInterval每隔x秒刷新一次内容。 It works on every PC browser, but not on iPhone/Android. 它适用于所有PC浏览器,但不适用于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)? 我知道这不是理想的解决方案,但是您可以使用HTML5辅助程序代替setInterval(尽管我也不知道它们是否也在后台暂停了-我假设它们不是在后台暂停,如果他们是)?

And it does add lots of backwards compatibility issues for PC browsers that are not HTML5 compliant :( 对于不兼容HTML5的PC浏览器,确实增加了许多向后兼容的问题:(

http://www.sitepoint.com/javascript-threading-html5-web-workers/ 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. 可能的解决方法是使用比您希望的X秒刷新时间更短的间隔,但是仅在经过适当的时间后才实际触发回调。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM