javascript 如何防止 iPhone(包括 iOS 7)在 HTML 或 JS 中进入睡眠状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18905413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:38:44  来源:igfitidea点击:

How can I prevent iPhone (including iOS 7) from going to sleep in HTML or JS?

javascripthtmliphoneiosauto-lock

提问by lanewinfield

I'm attempting to write some code to keep a phone alive and not go to sleep on a webpage.

我正在尝试编写一些代码来使手机保持活动状态而不是在网页上睡觉。

In my search, I found this post: Prevent iOS mobile safari from going idle / auto-locking / sleeping?

在我的搜索中,我发现了这篇文章:防止 iOS 移动 Safari 进入空闲状态/自动锁定/睡眠?

But looping an audio file seems to no longer keep MobileSafari alive and prevent the phone from locking. While forcing the page to refresh every 30 seconds works, I need the original page to stay open.

但是循环播放音频文件似乎不再让 MobileSafari 保持活动状态并阻止手机锁定。虽然强制页面每 30 秒刷新一次有效,但我需要原始页面保持打开状态。

Google's latest interactive music video, Just A Reflektor, seems to be preventing lock from mobile, and their JS herereferences a preventSleepIos function.

谷歌最新的互动音乐视频Just A Reflektor似乎在阻止手机锁定,他们的JS 在这里引用了 preventSleepIos 函数。

What could I simply do to prevent iOS from falling asleep?

我可以简单地做些什么来防止 iOS 入睡?

Thanks!

谢谢!

回答by Ioulian Alexeev

If you run the minified script through http://jsbeautifier.org/you'll get the idea of how this hack works.

如果您通过http://jsbeautifier.org/运行缩小的脚本,您将了解此 hack 的工作原理。

The idea of the hack is the following: If a new page is requested in safari, the ios device will reset the sleep timeout.

hack 的思路如下:如果在 safari 中请求新页面,ios 设备将重置睡眠超时。

Knowing that, we can can set an interval to request a new page each 30 seconds or so:

知道了这一点,我们可以设置一个间隔,每 30 秒左右请求一个新页面:

iosSleepPreventInterval = setInterval(function () {
    window.location.href = "/new/page";
}, 30000);

Now we need to stop the request so the page will be not redirected:

现在我们需要停止请求,这样页面就不会被重定向:

iosSleepPreventInterval = setInterval(function () {
    window.location.href = "/new/page";
    window.setTimeout(function () {
        window.stop()
    }, 0);
}, 30000);

Now there will be a request each 30 seconds to a page, so the ios device will not be put to sleep, and the request will be cancelled, so you don't navigate away from the page.

现在每 30 秒会有一个页面请求,因此 ios 设备不会进入睡眠状态,请求将被取消,因此您不会离开页面。

Note: I use this code for the "/new/page":

注意:我将此代码用于“/new/page”:

sleep(10);exit;

This hack has been tested on iOS 6 and iOS 7. You can test it yourself on jsBin.

此 hack 已在 iOS 6 和 iOS 7 上进行了测试。您可以在 jsBin 上自行测试

Note2: Android uses different hack to prevent the device from sleeping.

注意 2:Android 使用不同的 hack 来防止设备休眠。

回答by Patrick

You are going to have to make a request to a server, which is unfortunately not possible with strictly HTML or JS. Both of these are front-end scripting languages, meaning, that once the DOM is loaded, manipulation of the DOM with vanilla HTML and CSS will only affect the front-end.

您将不得不向服务器发出请求,不幸的是,这对于严格的 HTML 或 JS 是不可能的。这两种都是前端脚本语言,这意味着一旦 DOM 被加载,使用普通 HTML 和 CSS 对 DOM 的操作只会影响前端。

As far as I know, there are only a handful of ways to prevent iOS from going to sleep, one of which is to make a toy app that gets a service in iOS over and over, and the other would be to set application.idleTimerDisabled = YES, but both of these solutions are out of the scope of web front-end technologies.

据我所知,防止iOS进入休眠的方法只有少数几种,其中一种是制作一个玩具应用程序,在iOS中一遍遍地获取服务,另一种是设置application.idleTimerDisabled = YES,但两者都这些解决方案中的一部分超出了 Web 前端技术的范围。

If you want to prevent a session from being lost through a web app, you could write a cheap server side function to ping the time of the server every so often so as to preserve the user's session.

如果您想防止会话通过 Web 应用程序丢失,您可以编写一个廉价的服务器端函数来每隔一段时间 ping 服务器的时间,以保留用户的会话。

Example using javascript and PHP:

使用 javascript 和 PHP 的示例:

function cd(){
   var alerttime = "<?php echo date('h:i:s', (strtotime($_SESSION['ordertime']) + (1 * 60)));  ?>"
   var extratime = "<?php echo date('h:i:s', (strtotime($_SESSION['ordertime']) + (2 * 60)));  ?>";
    redo();
}    

function getTime(){
    currenttime = "<?php echo date('h:i:s', time()); ?>";

   var cd = setTimeout("getTime()",1000);
    }