long-poll jQuery.ajax() fails to callback after phone sleeps? long-poll jQuery.ajax() fails to callback after phone sleeps? google-chrome google-chrome

long-poll jQuery.ajax() fails to callback after phone sleeps?


I've had problems in the past with JavaScript calls getting suspended when the phone goes to sleep. The solution I ended up with was to use window.setInterval() which seems to suspend, but come back to life when the phone is woken up.

So I would recommend setting an interval which cancels the call every so often and reinitiates it. This might help it survive through a phone sleep.

Something roughly like:

var myCall = $.ajax({...});Window.setInterval (refreshCall(), 10000);function refreshCall (){    myCall.abort ();    myCall = $.ajax({...});}


I not sure this will work or not, I cannot test it now but give it a try

$(window).focus(function() {    updater();});


How about a higher-level watcher function like this:

var restartTimer = null;function updater(){    $.ajax({        type: "POST",        url: "/listen",        data: version,        success: function (data) {            version = handleUpdates(data);            clearTimeout(restartTimer);            updater();        },        error: function () {            clearTimeout(restartTimer);            setTimeout(updater, 1000);        }    });}//  Kick it when the phone wakes up.$(window).focus(function(){    restartTimer = setTimeout(function(){        initializeAll();    }, 6000);    updater();});

You know that the $(window).focus will fire when the phone wakes up, so you try updater() as Almis suggests, but with a fail-safe timer. If updater fires (on laptops or iOS), the timer is canceled and all is well, but if updater is dead, the fail-safe timer fires in 6 seconds and reboots your entire app by calling initializeAll().