I saw this everywhere on the web but i couldn't manage to fix my code to avoid this issue, simply I have an ajax function that I trigger when some buttons are clicked, sometimes I want an indicator (loading animation) to show, sometimes not, so i build my function:
function doAjax(action, todo, items, error_num, hide_indicator) {
items.action = action;
items.do = todo;
var postedObj;
$.ajax({
type: 'post',
dataType: 'html',
url: ajaxURL,
data: items,
async: false,
beforeSend: function() {
if (!hide_indicator) showIndicator();
},
success: function(data) {
if (data) {
ajaxObj = JSON.parse(data);
if (ajaxObj.ok) {
postedObj = ajaxObj;
} else {
alert(ajaxObj.error);
postedObj = false;
}
} else {
alert('[' + error_num + '] Something went wrong.');
postedObj = false;
}
if (!hide_indicator) hideIndicator();
},
error: function() {
alert('[' + error_num + '] Something went wrong.');
postedObj = false;
if (!hide_indicator) hideIndicator();
}
});
return postedObj;
}
And here is what I do on my buttons to call the function:
$(document).on('click', '.ad-single', function() {
var addataObj = doAjax('post_requests', 'get-ad-info', {"id": $(this).data('ad')}, '158', false); // false means DON'T hide the indicator
if (addataObj) {
loadContent(addataObj.ad);
}
});
OK, for now, everything works as expected on Firefox, when I click my button, the indicator shows up and wait until data is returned by ajax, then hide the indicator again.
This doesn't work on Chrome and Safari, the function works fine and return the data as expected but it seems that the hideIndicator()
function is called immediately, I couldn't know how to fix this.
Firefox:
Chrome and Safari: