I'm trying to run a function before async ajax request. However function is running after async request gets the respond.
Is there any way to solve this issue?
block();
ajaxRequest= $.ajax({
url: siteURL+'includes/ajax/action.php',
type: "POST",
async: false,
data: {productID : productID},
dataType: "json"
});
ajaxRequest.done(function(data) {
block(true);
if (data === false) {
alerts('error title','error info here', 'error', 200);
return false;
}
});
ajaxRequest.fail(function(jqXHR, textStatus) {block(true); alerts('error title','error info','error');});
confirm();
I run more functions after these codes. However as I stated before, block(); function is waiting till async ajax request is getting response.
If I don't run asynchronous, then I get block() and confirm() functions running at the same time so return false; losing all the meaning.
P.S. I run these codes when a form is submitted so if async request is failed I don't want it to run any other code after it. However when it is asynchronously running block() is waiting till response is returned.
block()is running before the request is sent.block()is performing a graphical or dom change, it may not appear to happen before the ajax request due toasync:falsestopping browser rendering.