Good for this question, I will ignore the excuse for why you need synchronous XHR requests. I understand that sometimes working constraints do not allow the use of a best practice solution, and therefore we “do” to do the work. So let's focus on how to get a synchronous ajax with a visual update that works for you!
Well, given that you are using jQuery for your XHR query, I am going to use it to use jQuery to show / hide the progress bar and handle any time issues.
First, configure the download indicator in our markup:
<div id="loading" style="display:none;">Loading...</div>
Now create some javascript:
// Show the loading indicator, then start a SYNCRONOUS ajax request $('#loading').show().delay(100).queue(function() { var jqxhr = $.ajax({type:"GET",dataType:"json",url:"www.yoururl.com/ajaxHandler",async: false}).done(function(){ //Do your success handling here }).fail(function() { //Do your error handling here }).always(function() { //This happens regardless of success/failure $('#loading').hide(); }); $(this).dequeue(); });
First, we want to show our loading indicator, and then give the browser an instant delay for redrawing before our synchronous XHR request starts. Using the jQuery .queue() method, we put our .ajax() call in the default fx queue so that it does not execute until .delay() , which of course does not happen until after .show() .
The jQuery .show() method changes the display style of the style of the .show() element to block (or restores its initial value, if assigned). This change in CSS will cause the browser to overpay (aka redraw) as soon as it can. The delay ensures that he can complete the payment before calling ajax. The delay is not needed in all browsers, but it will not hurt more than the number of milliseconds you specified (as usual, IE will be the limiting factor here, other browsers are happy with the 1 ms delay, IE wanted something a little more significant for repainting) .
Here's jsfiddle for testing in multiple browsers: jsfiddle example