开发者

Javascript stops as I lock iphone, can it still run?

Is there a way to tell a javascript function to co开发者_StackOverflow社区ntinue running even as a phone is locked?

Pretty much I have a timer that I need to continue to run even as the phone is set to idle.


Yes, you can. You can even run a JavaScript when the safari is in the background.

Just put a hidden frame on your site:

<iframe src="background.html" style="display:none;"></iframe>

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

Be careful because it can lead to high data traffic also if you are not on WiFi!


EDIT: You can run timed javascript code in the background using the <iframe> trick. See Zsolt's response to this question.

You cannot continue to run javascript while the iPhone is sleeping using setTimeout(), however.

When the phone is put to sleep, Safari will kill any running javascript processes using setTimeout(). Check out this answer here for some reasons why this is done.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜