I think if you're looking for a proper solution to this type of issue you should try out this kind of stuff to prevent this kind of issue...
1. create a function that sets a loader in your page with a unique loader id, and returns that unique id.
2. create another function to close the loader by that unique loader id.
3. You should be aware of the let & var declaration with JavaScript.
For example
function initPageLoader(){
let min = 100000, max = 999999;
let randomLoaderId = "fullPageLoader" + ( Math.floor(Math.random() * (max - min + 1) ) + min) + "Id";
let loader = $("<div>").addClass("fullpage-loading").attr({"id":randomLoaderId})
$("body").prepend(loader);
return randomLoaderId;
}
function hidePageLoader(loaderId){
$("#"+loaderId).remove();
}
function getDataUsingAJAX(){
let loaderID;
$.ajax({
type : "POST",
data : {data: JSON.stringify(formData)},
URL : ".../save.html",
dataType: 'JSON',
beforeSend: function( xhr ) {
loaderID = initPageLoader();
},
success : function(res) {
},
error : function(xhr, status, errorThrown) {
console.log(xhr, status, errorThrown);
},
complete:function(res){
hidePageLoader(loaderID);
}
});
}
This will helps you.