当 iPhone/Android 进入睡眠状态时 JavaScript 执行(settimeout 等)会发生什么? [英] What happens to JavaScript execution (settimeout, etc.) when iPhone/Android goes to sleep?

查看:22
本文介绍了当 iPhone/Android 进入睡眠状态时 JavaScript 执行(settimeout 等)会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个针对 iOS 和 Android 设备的 jQuery Mobile Web 应用程序.应用程序的一个组件是后台任务,它定期检查 a.) 本地数据的更改和 b.) 与服务器的连接.如果两者都为真,则任务推送更改.

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.

我正在使用一个简单的基于 setTimeout() 的函数来执行此任务.每个失败或成功条件都会调用后台任务的 setTimeout(),确保它以 30 秒的间隔运行.出于调试目的,我使用上次任务运行时的时间戳更新状态 div.

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.

在任何桌面浏览器中,这都能正常工作;但是,在 iOS 或 Android 上,一段时间后,任务停止执行.我想知道这是否与设备的省电设置有关——当 iOS 进入待机状态时,它是否会终止 JavaScript 执行?看起来就是这样.

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).

推荐答案

当浏览器页面未获得焦点时,似乎 Javascript 执行在 MobileSafari 上暂停.似乎如果 setInterval() 事件迟到了,只要浏览器聚焦,它们就会被触发.这意味着我们应该能够保持 setInterval() 运行,并假设如果 setInterval 函数花费的时间比平时长得多,浏览器会失去/重新获得焦点.

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.

此代码在从浏览器选项卡切换回、从另一个应用切换回以及从睡眠状态恢复后发出警报.如果您将阈值设置得比 setTimeout() 长一点,则您可以假设超时不会在触发时完成.

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.

如果您想保持安全:您可以保存您的超时 ID(由 setTimeout 返回)并将其设置为比您的超时更短的阈值,然后在触发时再次运行 clearTimeout() 和 setTimeout().

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>

看起来这有时会在恢复时连续触发多次,因此您需要以某种方式处理它.(在让我的 android 浏览器整夜睡眠后,它醒来了两个警报().我敢打赌 Javascript 在完全睡眠之前的某个任意时间恢复了.)

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.)

我在 Android 2.2 和最新的 iOS 上进行了测试 - 一旦您从睡眠中恢复,它们都会发出警报.

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

这篇关于当 iPhone/Android 进入睡眠状态时 JavaScript 执行(settimeout 等)会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆