ios - What happens to JavaScript execution (settimeout, etc.) when iPhone/Android goes to sleep? -


i have jquery mobile web app targets ios , android devices. component of application background task, periodically checks a.) changes local data , b.) connectivity server. if both true, task pushes changes.

i'm using simple settimeout()-based function execute task. each failure or success condition calls settimeout() on background task, ensuring runs on 30 second intervals. update status div timestamp of last task runtime debugging purposes.

in desktop browser, works fine; however, on ios or android, after period of time, task stops executing. i'm wondering if related power conservation settings of devices--when ios enters stand-by, terminate javascript execution? appears happen.

if so, best way resume? there on-wake event can hook into? if not, other options there don't involve hooking events dependent on user interaction (i don't want bind entire page click event restart background task).

looks javascript execution paused on mobilesafari when browser page isn't focused. seems if setinterval() events late, fired browser focused. means should able keep setinterval() running, , assume browser lost/regained focus if setinterval function took longer usual.

this code alerts after switching browser tab, after switching app, , after resuming sleep. if set threshold bit longer settimeout(), can assume timeout wouldn't finish if fires.

if wanted stay on safe side: save timeout id (returned settimeout) , set shorter threshold timeout, run cleartimeout() , settimeout() again if fires.

<script type="text/javascript"> var lastcheck = 0;  function sleepcheck() {         var = new date().gettime();         var diff = - lastcheck;         if (diff > 3000) {                 alert('took ' + diff + 'ms');         }         lastcheck = now; }  window.onload = function() {         lastcheck = new date().gettime();         setinterval(sleepcheck, 1000); } </script> 

edit: appears can trigger more once in row on resume, you'd need handle somehow. (after letting android browser sleep night, woke 2 alert()s. bet javascript got resumed @ arbitrary time before sleeping.)

i tested on android 2.2 , latest ios - both alert resume sleep.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -