当我锁定 iphone 时 Javascript 停止,它还能运行吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7047989/
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
Javascript stops as I lock iphone, can it still run?
提问by Ben Potter
Is there a way to tell a javascript function to continue running even as a phone is locked?
有没有办法让 javascript 函数在手机被锁定时继续运行?
Pretty much I have a timer that I need to continue to run even as the phone is set to idle.
几乎我有一个计时器,即使手机设置为空闲,我也需要继续运行它。
回答by Zsolt Hari
Yes, you can. You can even run a JavaScript when the safari is in the background.
是的你可以。当 safari 在后台时,您甚至可以运行 JavaScript。
Just put a hidden frame on your site:
只需在您的网站上放置一个隐藏的框架:
<iframe src="background.html" style="display:none;"></iframe>
background.html:
背景.html:
<!DOCTYPE HTML>
<head>
<meta http-equiv="refresh" content="3" />
</head>
<body>
<script>
parent.run_function_from_core_page();
</script>
</body>
</html>
The page in the hidden frame refreshes every 3 seconds, and run the JavaScript, even if the phone is locked.
隐藏框中的页面每 3 秒刷新一次,并运行 JavaScript,即使手机被锁定。
Be careful because it can lead to high data traffic also if you are not on WiFi!
小心,因为如果您不在 WiFi 上,它也会导致高数据流量!
回答by mopsled
EDIT: You can run timed javascript code in the background using the <iframe>
trick. See Zsolt's response to this question.
编辑:您可以使用该<iframe>
技巧在后台运行定时 javascript 代码。请参阅Zsolt 对此问题的回答。
You cannot continue to run javascript while the iPhone is sleeping using setTimeout()
, however.
setTimeout()
但是,当 iPhone 处于睡眠状态时,您无法继续运行 javascript 。
When the phone is put to sleep, Safari will kill any running javascript processes using setTimeout()
. Check out this answer herefor some reasons why this is done.
回答by Jonathan Grynspan
Nope. When the phone is locked and goes to sleep, your code will not run. This is by design; iOS devices are essentially cell phones, not full-fledged PCs, and are designed to save power when the user is not using them.
不。当手机锁定并进入睡眠状态时,您的代码将不会运行。这是设计使然;iOS 设备本质上是手机,而不是成熟的 PC,旨在在用户不使用它们时节省电量。