On my website I use a long polling jquery ajax function. This ajax call in this function has a 50 seconds time-out and then runs again, waiting for changes on the server.
However, sometimes the long polling http connection stops. This happens for instance when the internet connection is down, or when the client pc is turn on after sleep or hibernate. The problem with this is that the "complete" callback doesnt occur yet, the $.ajax() function is still waiting for the timeout, while the http connection doesnt exist anymore.
How can check with jquery/javascript if the connection is still open?
This is my long poller script:
function longPoller() {
$.ajax({
type: "GET",
url: '/longpolltest2.php',
dataType: 'json',
async: true,
cache: false,
timeout:50000, /* Timeout in ms */
success: function(data){
alert('success');
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('error');
},
complete: function() {
alert('complete');
longPoller(); // restart long poller request
}
});
}
@jAndy, I have updated the script, but I receive the following error:
Permission denied for http://domain.com to create wrapper for object of class UnnamedClass
function masterLongPollerDebugger() {
xhr = $.ajax({
type: "GET",
url: '/longpolltest2.php',
dataType: 'json',
async: true,
cache: false,
timeout:50000,
success: function(data){
alert('success');
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('error');
},
complete: function() {
alert('complete');
masterLongPollerDebugger();
}
});
}
function ajaxRunning() {
xhr._onreadystatechange = xhr.onreadystatechange; // store a reference
xhr.onreadystatechange = function()
{ // overwrite the handler
xhr._onreadystatechange(); // call the original stored handler
alert(xhr.readyState);
if (xhr.readyState === 3)
alert('Interactive');
};
}