Dave Moz
Dave Moz

Reputation: 127

Loop jquery $.get, how to know when loop is finished

I'm trying to do a loop where I load some content from another page using jquery $.get.

Here is what I am doing:

for(var i=0; i<users.length; i++) {
    var myURL = "http://url.com/user.php?user=";

    myURL = myURL + users[i];

    $.get(myURL, function(data){
        $('table').append( data )
    });

}

That part all works fine, it updates my table with the data returned. My problem is that I want to show a loading icon, or even just text that says "loading" until it finishes loading all the data from the other page.

I'll spare you from all the things I have tried that don't work, because I really suck at JS, and it will be embarrassing.

Any suggestions would be appreciated.

Upvotes: 0

Views: 361

Answers (1)

user684934
user684934

Reputation:

var doneCounter = 0;
for(var i=0; i<users.length; i++) {
    var myURL = "http://url.com/user.php?user=";

    myURL = myURL + users[i];

    $.get(myURL, function(data){
        $('table').append( data );
        doneCounter++;
        if(doneCounter == users.length)  hideBusy(); 
    });

}

Upvotes: 1

Related Questions