Detect AJAX abort due to page unload #1280

This commit is contained in:
Kijin Sung 2020-05-21 20:53:49 +09:00
parent 766cd4b1d5
commit 953faecd55

View file

@ -15,6 +15,11 @@
*/
window.show_leaving_warning = false;
/**
* This variable becomes true when the user tries to navigate away from the page.
*/
var page_unloading = false;
/**
* This variable stores the .wfsr jQuery object.
*/
@ -127,6 +132,11 @@
return xmlHandler(xhr, textStatus);
}
// If the user is navigating away, don't do anything.
if (xhr.status == 0 && page_unloading) {
return;
}
// Hide the waiting message and display an error notice.
clearTimeout(wfsr_timeout);
waiting_obj.hide().trigger("cancel_confirm");
@ -266,6 +276,13 @@
// Define the error handler.
var errorHandler = function(xhr, textStatus) {
// If the user is navigating away, don't do anything.
if (xhr.status == 0 && page_unloading) {
return;
}
// Hide the waiting message and display an error notice.
clearTimeout(wfsr_timeout);
waiting_obj.hide().trigger("cancel_confirm");
var error_info;
@ -338,6 +355,13 @@
// Define the error handler.
var errorHandler = function(xhr, textStatus) {
// If the user is navigating away, don't do anything.
if (xhr.status == 0 && page_unloading) {
return;
}
// Hide the waiting message and display an error notice.
clearTimeout(wfsr_timeout);
waiting_obj.hide().trigger("cancel_confirm");
var error_info = xhr.status + " " + xhr.statusText + " (" + textStatus + ")";
@ -423,6 +447,7 @@
* Empty placeholder for beforeUnload handler.
*/
var beforeUnloadHandler = function() {
page_unloading = true;
return "";
};
@ -438,6 +463,10 @@
}).bind("ajaxStop cancel_confirm", function() {
$(window).unbind("beforeunload", beforeUnloadHandler);
});
} else {
$(window).on('beforeunload', function() {
page_unloading = true;
});
}
});