Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there a way to tell a javascript function to continue 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.

like image 878
Ben Potter Avatar asked Aug 13 '11 01:08

Ben Potter


1 Answers

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!

like image 151
Zsolt Hari Avatar answered Oct 15 '22 04:10

Zsolt Hari