19

I have a page that has a tab set. Each of the tabs is loaded by the jQuery .load() function.

I want to display a loading animation that disappears when all of the ajax requests are finished. However, document.ready() has only provided me with limited success.

How can I ensure that all ajax requests are completed before executing the code to hide the loading animation?

Bart
  • 19,692
  • 7
  • 68
  • 77
Mike
  • 199
  • 1
  • 1
  • 3
  • RELATED: [Waiting on multiple asynchronous calls to complete before continuing](http://stackoverflow.com/questions/2768293/waiting-on-multiple-asynchronous-calls-to-complete-before-continuing) – Justin Obney Feb 28 '12 at 16:43

7 Answers7

33
.ajaxStop(handler)

Documentation here: http://api.jquery.com/ajaxStop/

JC Ford
  • 6,946
  • 3
  • 25
  • 34
9
$(document).ready(function () {
     $(document).ajaxComplete(function () {
         alert('Completed');
     });
});
KittMedia
  • 7,368
  • 13
  • 34
  • 38
Zubair sadiq
  • 500
  • 6
  • 23
  • This is really good. I used the $(document).ajaxComplete(function(){...}) , but without the document.ready. Works well for IE8 (as opposed to using setTimeout() ). – goamn Jan 23 '17 at 06:36
  • ajaxComplete executes after every AJAX request has completed so it would be invoked multiple times. ajaxStop would execute after all AJAX requests have completed, invoking it only once. – Mubashar Aftab Sep 10 '21 at 15:30
5

ajaxComplete

Per the documentation:

$('.log').ajaxComplete(function() {
    $(this).text('Triggered ajaxComplete handler.');
});
Jason
  • 51,583
  • 38
  • 133
  • 185
  • 3
    Well, this will fire on completion of each AJAX request. I read the question as "How to detect when they are all finished?" – Matt Mar 23 '10 at 19:06
4

Use the callback argument to .load(), setting a flag or increasing a counter in the callback function. Once all flags are set or the counter reaches the number of tabs, you know all tabs have been loaded, and you can remove the animation.

In pseudocode that might or might not be valid JavaScript:

loadedTabs = 0;

function onLoad() {
    for (int i = 0; i < numTabs; ++i) {
        tabs[i].load(tabUrl(i), tabLoaded);
    }
}

function tabLoaded() {
    ++loadedTabs;
    if (loadedTabs == numTabs)
        loadingAnimation.display = 'none';
}
Thomas
  • 174,939
  • 50
  • 355
  • 478
  • Good approach, but how would I know when the right number has been reached? Writing a `while` loop which waits for the right number to be set seems like a bad idea for some reason... – Mike Mar 23 '10 at 19:09
  • Ah! I see what you're saying... I'll give this a try to tomorrow and see how it works! Thanks so much! – Mike Mar 23 '10 at 19:32
3

Basically, I have almost similar of this issue, which I want to load a Grid after completing load 2 combo boxes and at the end I want to load a grid, so I solved like that

    function LoadDropbox1()
    {
        //ajax1 load first dropbox
    }

    function LoadDropbox2()
    {
        //ajax2 load Second dropbox
    }

    function LoadGrid()
    {
        //ajax3 load Grid after complete the two drops loading...
    }

    $(document).ready(function () {
        LoadDropbox1();
        LoadDropbox2();
    });

    var Executed = false;

    jQuery(function ($) {
        $(document).ajaxStop(function () {
            // Executed when all ajax requests are done.
            if (!Executed) LoadGrid();
            Executed = true;
        });
    });
Adel
  • 1,468
  • 15
  • 18
0

Look at this post and answers... https://stackoverflow.com/a/13090589/999958

A useful workaround for me: Look at ajaxCallComplete() call in each .complete(...);

$(document).ready(function(){
    loadPersons();
    loadCountries();
    loadCities();
});

// Define how many Ajax calls must be done
var ajaxCalls = 3;
var counter = 0;
var ajaxCallComplete = function() {
    counter++;
    if( counter >= ajaxCalls ) {
            // When all ajax calls has been done
        // Do something like hide waiting images, or any else function call
        $('*').css('cursor', 'auto');
    }
};

var loadPersons = function() {
        // Show waiting image, or something else
    $('*').css('cursor', 'wait');

    var url = global.ctx + '/loadPersons';
    $.getJSON(url, function(data) {
            // Fun things
    })
    .complete(function() { ajaxCallComplete(); });
};

var loadCountries = function() {
    // Do things
    var url = global.ctx + '/loadCountries';
    $.getJSON(url, function(data) {
            // Travels
    })
    .complete(function() { ajaxCallComplete(); });
};

var loadCities = function() {
    // Do things
    var url = global.ctx + '/loadCities';
    $.getJSON(url, function(data) {
            // Travels
    })
    .complete(function() { ajaxCallComplete(); });
};

Hope can help...

Community
  • 1
  • 1
Jesfre
  • 702
  • 8
  • 13
0
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);

function BeginRequestHandler(sender, args) {
    $modal.show();
    $overlay.show();
}

function EndRequestHandler(sender, args) {
    $modal.hide();
    $overlay.hide();

}
456qwe123asd
  • 191
  • 4
  • 17