Mark Hollas
Mark Hollas

Reputation: 1137

jquery how to know when each loop complete

I am working with the open graph API and trying to run a each loop through a results callback.

I need to know when the loop is complete so I can run a 'success' function.

I thought it would be as easy as comparing the results length against the index but I am having no luck.

What is the best way to do this?

function authSuccess() {
    FB.api('/me/friends', function (result) {
        if (result.data) {
         var l = result.data.length;
         $.each(result.data, function (index, friend) {
                if(l > index ){
                      //do stuff
                }else{
                    //go to success
                     }
            });
        } else { }
    });
}

Thanks for the help

Upvotes: 0

Views: 3847

Answers (2)

Man Programmer
Man Programmer

Reputation: 5356

function authSuccess() {
    FB.api('/me/friends', function (result) {
        if (result.data) {
         var len=0;
         $.each(result.data, function (index, friend) {
                if(!result.data[len+1] ){
//go to success                    

                }else{
                    //do stuff
                     }
len++;
            });
        } else { }
    });
}

Upvotes: 0

Miha Rekar
Miha Rekar

Reputation: 1546

This ought to do it:

$.each(result.data, function (index, friend) {
//do your work
});
//go to success

Upvotes: 2

Related Questions