You are checking for specific status "timeout". Add else statement for handling other statuses.
Ajax polling stops to poll after internet disconnect
-
14-10-2022 - |
سؤال
I have a sort of ajax polling cycle - pull message, process message, wait, pull again ... - everything works fine until user disconnects from the internet (it can be even so simple user action as sleep/hibernating the PC). After that - at least in latest Chrome - POST fails (logicly) - but after reconnecting to the internet the poll cycle does not get started again (even when the session is still ON).
jQuery(function(){
// ba polling main
function pollingMain() {
$.ajax({
type: "POST",
url: ba_home_url+"/pollingscript.php",
data: {
"load": "something",
"data-one": 0,
"data-two": 0
},
dataType: "json",
timeout: 60000,
success: function(data) {
if( data.status == 'nodata' ){
setTimeout(pollingMain,10000);
} else{
if( data.status == 'okdata' ){
console.log('Ok data recived');
setTimeout(pollingMain,10000);
} else{
//server responded with something we dont know what is
setTimeout(pollingMain,10000);
}
}
},
error: function(request, status, err) {
if(status == "timeout") {
setTimeout(pollingMain,10000);
}
}
});
}
pollingMain();
});
That is my code. I guess I am missing something in the error callback - but what error status handling should I add there ?
المحلول
لا تنتمي إلى StackOverflow