简体   繁体   English

如何防止iPhone(包括iOS 7)在HTML或JS中入睡?

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

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移动游侠闲置/自动锁定/睡眠?

But looping an audio file seems to no longer keep MobileSafari alive and prevent the phone from locking. 但循环播放音频文件似乎不再使MobileSafari保持活动状态并阻止手机锁定。 While forcing the page to refresh every 30 seconds works, I need the original page to stay open. 在强制页面每隔30秒刷新一次的同时,我需要原始页面保持打开状态。

Google's latest interactive music video, Just A Reflektor , seems to be preventing lock from mobile, and their JS here references a preventSleepIos function. 谷歌的最新互动音乐视频Just A Reflektor似乎阻止了移动锁定,他们的JS在这里引用了preventSleepIos功能。

What could I simply do to prevent iOS from falling asleep? 我可以做些什么来阻止iOS入睡?

Thanks! 谢谢!

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. 黑客的想法如下:如果在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来防止设备休眠。

You are going to have to make a request to a server, which is unfortunately not possible with strictly HTML or JS. 您将不得不向服务器发出请求,遗憾的是严格的HTML或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. 这两种都是前端脚本语言,这意味着,一旦加载了DOM,使用vanilla 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);
    }

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

相关问题 html css 和 javascript - 如何防止我的车离开屏幕 - html css and javascript - How can I prevent my car from going out of the screen 如何阻止Require.js优化器在优化文件中包含文本插件? - How can I prevent the Require.js optimizer from including the text plugin in optimized files? 我可以防止手机在网页上休眠吗 - Can I prevent phone from sleep on a webpage 如何防止 Jquery 对话框离开页面? - How can I prevent a Jquery dialog from going off the page? HTML5和Javascript,iPhone文件上传,防止手机进入睡眠状态 - HTML5 & Javascript, iPhone File Upload, Preventing phone from going to sleep 如何防止 PHP sleep function 停止我的 HTML 页面 - How do I prevent PHP sleep function from stopping my HTML page 如何从进入睡眠状态的WebSocket客户端计算机或应用程序进入后台恢复(iPad上的Safari) - How do I recover from a WebSocket client computer going to sleep or app going to background (Safari on iPad) 如何防止我的表单操作 (html) 使用 javascript? - How do I prevent my form action (html) from going through with javascript? ExtJS:如何防止GridPanel中的列数之和超过给定的最大值? - ExtJS: How can I prevent the sum of number columns in a GridPanel from going over a given maximum? react - 防止视频在 iOS 上全屏显示 - react - prevent video from going fullscreen on iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM