Before calling ajax you need to show the loading display and you can use the callback and success function to hide the loading display
//show here the loading display $(".loading").show(); setTimeout(function() { var jqXHR = $.ajax({ type: "POST", url: url, cache: false, data: params, // array of parameters async: false, // responseText is empty if set to true dataType: 'json', error: function(){ alert("Ajax post request to the following script failed: " + url); }, success: function(){ //hide loading display here $(".loading").hide(); } }); }, 0);
well, you need a delay using setTimeout() before calling the ajax function, because it can even stop the loading display from showing, because as soon as $(".loading").show(); animated $(".loading").show(); , ajax synchronization request will be called and, of course, will block the browser until the animation of the loading display is completed
source share