I have some (potentially) long-running ajax calls that I would like to abort if the user navigates to another page. The following jQuery code calls abort on all pending XMLHttpRequest objects upon navigating away from the page:
$.ajaxSetup({
beforeSend: function(xhr) {
$(window).bind('beforeunload', function() {
xhr.abort();
});
}
});
In a test case, I force a 10-second wait on the server-side operation being called. Using Firebug, I confirmed that the above code does indeed cause all pending ajax calls to halt immediately when I click any link on the page. However, the browser still waits the full 10 seconds before moving on to the next page. IE appears to exhibit the same behavior. Is this a known browser behavior? Is there anything I can do allow the user to navigate away from the page immediately in this situation? Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…