Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Indicator on Synchronous Ajax

I'm using ajax with jQuery on my site and need to show a progress / loading indicator.

My dilemna is this:

  • Synchronous AJAX locks the browser, so I cant do anything (e.g. show a loading indicator) till the contents are returned, by which time it is too late
  • I am using JSON as the return data type and setting async = true returns an empty response string
  • my whole framework relies on the return type being JSON-formatted

I cannot seem to find any way to get JS to give the user an indication that something is in progress, except for doing an alert(). (For some reason an alert does work).

Any suggestions?

My code:

JS

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);
    }

} ); 

var html = jqXHR.responseText;

PHP

$return = array();
$return['status'] = 1;
$return['message'] = "some html here ...";
echo json_encode($return);
like image 581
little_edian Avatar asked May 17 '13 01:05

little_edian


People also ask

Can AJAX be made synchronous?

AJAX can access the server both synchronously and asynchronously: Synchronously, in which the script stops and waits for the server to send back a reply before continuing. Asynchronously, in which the script allows the page to continue to be processed and handles the reply if and when it arrives.

Is AJAX synchronous or asynchronous?

Ajax requests are Asynchronous by nature, but it can be set to Synchronous , thus, having the codes before it, execute first.

What is AJAX synchronous call?

Synchronous API call means Javascript thread will stop further execution of code until this Ajax request gets resolved. Since the main thread is blocked waiting for request to get completed, your browser will stop responding to events (it will become unresponsive).

Is AJAX same as asynchronous?

AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.


2 Answers

You can use the Jquery Global Ajax Event Handlers. This link describes them in detail:

http://api.jquery.com/category/ajax/global-ajax-event-handlers/

$(document).ajaxStart(function () {
    $("#loading").show();
});

$(document).ajaxComplete(function () {
    $("#loading").hide();
});
like image 156
Baahubali Avatar answered Oct 10 '22 21:10

Baahubali


You need to show a loading display before calling the ajax request and you can use the callback function and success to hide a 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 halt the display of the loading display because as while $(".loading").show(); is being animated the sync ajax request will be called and will surely lock the browser before the loading display animation will be completed

like image 36
Netorica Avatar answered Oct 10 '22 21:10

Netorica