Freddie
Freddie

Reputation: 11

Solving chained callbacks in Jquery

I try to understand whats happening within this code: (This is said to be a very effective way for solving chained callbacks)

(function hidenext(jq){
    jq.eq(0).fadeOut("fast", function(){
        (jq=jq.slice(1)).length && hidenext(jq);
    });

})($('div#bodyContent a'))

Would really appreciate some help!

Thanks, Freddie from Sweden

Upvotes: 1

Views: 131

Answers (1)

Michael Lorton
Michael Lorton

Reputation: 44386

Hallå Freddie from Sweden

Let me see if I can re-write it for you:

function hidenext(jq){
    jq.eq(0).fadeOut("fast", function(){
        jq=jq.slice(1);
        if (jq.length !== 0) {
           hidenext(jq);
        }
   });

};
hidenext($('div#bodyContent a'));

In words: given a list of elements, fade the first one out and when that fade completes, take the list that consists of everything but the first element and, if that list is non-empty, tail-recurse.

Hope this helps.

Michael from California

Upvotes: 2

Related Questions