I can't seem to get this ajax request to cancel when the user leaves the page. I'm not sure why. The problem is that the client is waiting for the request to complete before the user leaves the page. This is a large request in that it takes about 5 - 7 seconds to complete. As a result, if a user navigates away from this page while a request is under way they have to wait that time before the next page is loaded. Here is my code.
window.loader = setInterval(load_queued_tasks, 15000);
// function that should run every 15 seconds //
function load_queued_tasks() {
window.loader_ajax_request = $.ajax({
url: "coolpage.php",
type: 'GET',
success: function(data) {
//lots of exciting actions
})
},
error: function() {
return false;
}
}); //end ajax call
}
window.onbeforeunload = function (e) {
//clear the interval timer
window.clearInterval(window.loader);
//abort any running ajax request
window.loader_ajax_request.abort();
};