omega
omega

Reputation: 43853

how to check if all ajax functions are done loading in the page?

In my page I am using SharePoint 2010 client object model to call asynchronous functions (ajax calls). On page load, I make lots of ajax calls to load data. But I would like to know if there is a way so that a function can get called once all the ajax calls are done. Is there someway to do this? Thanks

Upvotes: 0

Views: 1770

Answers (2)

Gabe
Gabe

Reputation: 50493

A jquery solution, use $.active to see how many active requests there are.

So...

var ajaxCheckInterval = setInterval(checkAjaxState, 10);

function checkAjaxState(){
    if($.active == 0){
        clearInterval(ajaxCheckInterval);
        // do work
    }
}

Possibly something like this:

var ajaxCheckInterval = setInterval(checkAjaxState, 10);

function checkAjaxState(){
    if(document.readyState == 'complete'){
        clearInterval(ajaxCheckInterval);
        // do work
    }
}

Upvotes: -1

Alnitak
Alnitak

Reputation: 339816

If you're using jQuery to issue the AJAX calls, register an ajaxStop handler:

$(document).ajaxStop(function() {
    // this will be called when all running AJAX calls have completed
});

Upvotes: 4

Related Questions