[英]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.