If you try this example, you'll see why the async=false is the problem. (I made this JSFiddle for the example: jsfiddle.net/nmzu83wL/12 The URL makes a request to a server that takes 3 seconds to respond.)
$('.loading-gif').show();
$.ajax({
url: '/echo/js?js=&delay=3',
method: 'GET',
async: false
}).done(function() {
});
What you will notice here is that the loading indicator will be shown but only after 3 seconds. That is because the async=false is blocking and that blocking is also preventing the repaint needed to show the image from happening. This happens because not every DOM operation is instantaneous. Some operations, like repainting, wait for the main processing thread to yield before running.
I am positive that you will be able to write your logic in such a way that your constraints are still met but are done in an asynchronous way. I didn't entirely understand what you meant by "to be executed if or else the ajax call is made", but if you get stuck and you can clarify, I could propose a solution.
Matt Strom
Software Engineer, TypeScript ninja
The
{ async: false }is probably wrong... theasync=falseoption will cause the request to be blocking. I presume you did that because the loading indicator disappeared too quickly, right? If so, it disappeared too quickly because the image needs to be hidden only after the AJAX request has returned by using the callback.$('.loading-gif').show(); $ajax({ url: url }).done(function(){ //some operation $('.loading-gif').hide(); });