JSFiddle - React, Tailwind, and code Playground
by jfriend00
HTML
<button id="go">Start 4 second Ajax Call</button><br><br>
Start the 4 second Ajax call, then try any option that leaves the page such as closing the tab or clicking the link below or hitting F5 before the 4 seconds is up.<br><br>
<a href="http://jsfiddle.net">Go to jsFiddle.net</a><br>
JavaScript
var unloading = false;
$("#go").click(function() {
$.ajax({
url: '/echo/json/',
dataType: 'json',
type: 'POST',
global: false,
data: {
delay: 4,
json: JSON.stringify({msg:"Hello"})
},
success: function(data) {
alert("Success " + data.msg);
},
error: function(jqXHR, status, err) {
if (!unloading) {
alert("Error status " + status);
}
console.log("Error status " + status);
}
});
});
$(window).on("beforeunload", function() {
unloading = true;
});