Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Can Mobile Safari run JavaScript while my phone is in sleep-mode?

I hope others have had better luck on this topic than I.

As a test, I wrote some JavaScript to count every 1 second and display it.

CODE:

// define function and counter
var counter = 1;

function update_me() {
setTimeout(function(){
// update element
document.getElementById('counter').innerHTML = counter;
counter++;

update_me();
}, 1000);
}

// run code
update_me();

CODE: END

It works great when I keep Safari active, but my problem occurs in sleep-mode. I can view the counter, but as soon as I hit the button on the top of my phone the counter stops, even though Safari is running in the background. I can hit the button again, slide the bar and see the counter continue where it left off.

I'd like to know if its possible to keep JavaScript running while the phone is sleeping.



Thanks in advance everyone,


-Dane

3GS, iPhone OS 3.1.2

Posted on Jan 13, 2010 9:00 AM

Reply
4 replies

Jan 13, 2010 9:18 AM in response to wjosten

I read that one last night, but that's for writing an iPhone app where I'm trying to write a web app in Safari. Last night I did attempt to produce a similar effect in HTML as described in the Xcode example from your link. It didn't work because any audio embed/linked to on a web page must play in QuickTime, taking the user away from the web app screen.

Thank you for the quick response wjosten, we're thinking about it the same way. Any other thoughts?

Jan 13, 2010 10:52 AM in response to wjosten

Progress progress!

OK, so Mobile Safari will kill a JavaScript process after 10 seconds. That's smart because there shouldn't be a process that needs to run that long. If something needs to keep running use setTimeout(). See my above code snippet for an example on how to use it.

Mobile Safari stops JavaScript when in sleep-mode only when viewed in the Safari app directly. By this I mean in one of the 8 windows Safari allows you to have opened. I noticed that after adding a button on my home screen to my test app and launching it from there JavaScript continues to run while in sleep-mode.

My understanding of this behavior is that by adding/launching a page to the home screen the iPhone views the page as an app and no longer a website. This gives your code fewer constraints.


I hope this helps more than just me,



-Dane

Can Mobile Safari run JavaScript while my phone is in sleep-mode?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.