恰好JavaScript执行(setTimeout的,等)时,iPhone / Android的进入睡眠状态是什么?睡眠、状态、setTimeout、JavaScript

2023-09-03 20:46:29 作者:秋风画冷屏

我有一个jQuery Mobile的网络应用程序而针对iOS和Android设备。应用程序的一个组件是一个后台任务,其中定期检查一个。)改变为本地数据和b。)连接到该服务器。如果两个都为真,任务推动改变。

我用一个简单的setTimeout() - 基于函数来执行这个任务。每个失败或成功的条件上的背景任务调用的setTimeout(),确保它运行在第二间隔30。我更新的最后一个任务运行时的时间戳的状态DIV进行调试。

在任何桌面浏览器,这工作就好;然而,在iOS或Android,一段时间后,任务将停止执行。我想知道,如果这是关系到设备的节能设置 - 在iOS的进入待机,它终止JavaScript执行?那是什么似乎发生。

如果有,是什么恢复的最好方法是什么?是否有一个对唤醒事件,我可以挂接到?如果没有,还有什么其他选择有不涉及挂钩到用户交互相关的事件(我不想整个页面绑定到click事件只是重新启动后台任务)。

解决方案

看起来像Javascript的执行暂停在MobileSafari时,浏览器页面不集中。它也似乎如果setInterval的()事件是晚期,它们被简单地只要浏览器被聚焦烧制。这意味着我们应该能够保持一个的setInterval()的运行,并承担浏览器的丢失/如果setInterval函数了比平常更长的时间恢复了关注的焦点。

深入理解 JavaScript 执行上下文和执行栈

这code警报开关从一个浏览器标签页背面,从切换其他程序后回来后,和从睡眠状态恢复后。如果您设置的阈值多一点的时间比你的setTimeout(),你可以假设你的超时时间,如果这大火的话还没说完。

如果你想留在安全方面:您可以保存超时ID(由setTimeout的返回),并设置为一个较短的门槛比你超时,然后运行clearTimeout()和setTimeout的(),如果再这样火

 <脚本类型=文/ JavaScript的>
变种lastCheck = 0;

传播sleepCheck(){
        。VAR现在=新的日期()的getTime();
        VAR差异=现在 -  lastCheck;
        如果(DIFF> 3000){
                警报('花'+差异+MS);
        }
        lastCheck =现在;
}

在window.onload =功能(){
        lastCheck =新的日期()的getTime()。
        的setInterval(sleepCheck,1000);
}
< / SCRIPT>
 

编辑:看来这有时可能会触发一次以上一排的简历,所以你需要处理,不知怎的。 (让整个晚上我的Andr​​oid浏览器的睡眠后,醒来的时候两个警报()S。我打赌的Javascript得到了一些任意的时间前要充分睡眠恢复。)

只要你从睡眠状态恢复他们都警觉

-

我测试了在Android 2.2和最新的iOS。

I have a jQuery Mobile web app which targets iOS and Android devices. A component of the application is a background task, which periodically checks for a.) changes to local data and b.) connectivity to the server. If both are true, the task pushes the changes.

I'm using a simple setTimeout()-based function to execute this task. Each failure or success condition calls setTimeout() on the background task, ensuring that it runs on 30 second intervals. I update a status div with the timestamp of the last task runtime for debugging purposes.

In any desktop browser, this works just fine; however, on iOS or Android, after some period of time, the task stops executing. I'm wondering if this is related to the power conservation settings of the devices--when iOS enters stand-by, does it terminate JavaScript execution? That is what appears to happen.

If so, what is the best way to resume? Is there an on-wake event which I can hook into? If not, what other options are there which don't involve hooking into events dependent on user interaction (I don't want to bind the entire page to a click event just to restart the background task).

解决方案

Looks like Javascript execution is paused on MobileSafari when the browser page isn't focused. It also seems if setInterval() events are late, they are simply fired as soon as the browser is focused. This means we should be able to keep a setInterval() running, and assume the browser lost/regained focus if the setInterval function took much longer than usual.

This code alerts after switching back from a browser tab, after switching back from another app, and after resuming from sleep. If you set your threshold a bit longer than your setTimeout(), you can assume your timeout wouldn't finish if this fires.

If you wanted to stay on the safe side: you could save your timeout ID (returned by setTimeout) and set this to a shorter threshold than your timeout, then run clearTimeout() and setTimeout() again if this fires.

<script type="text/javascript">
var lastCheck = 0;

function sleepCheck() {
        var now = new Date().getTime();
        var diff = now - lastCheck;
        if (diff > 3000) {
                alert('took ' + diff + 'ms');
        }
        lastCheck = now;
}

window.onload = function() {
        lastCheck = new Date().getTime();
        setInterval(sleepCheck, 1000);
}
</script>

Edit: It appears this can sometimes trigger more than once in a row on resume, so you'd need to handle that somehow. (After letting my android browser sleep all night, it woke up to two alert()s. I bet Javascript got resumed at some arbitrary time before fully sleeping.)

I tested on Android 2.2 and the latest iOS - they both alert as soon as you resume from sleep.

 
精彩推荐
图片推荐