finding last loop through a jQuery object

Posted by DA on Stack Overflow See other posts from Stack Overflow or by DA
Published on 2010-03-15T23:15:09Z Indexed on 2010/03/15 23:19 UTC
Read the original article Hit count: 163

Filed under:

Sample jquery. Assume $cog is a cached selector of multiple items.

$cog.fadeOut('slow',function(){
    alert('hey');
})

In that example, of $cog is a jQuery object of 4 DOM elements, the above will fade each element out one by one, and trigger an alert each time on the callback (4 alerts).

I'd like to only call the alert when all 4 elements are done with their fadeOut function.

This:

$cog.fadeOut('slow',function(){
})
alert('hey');

when run, will show an alert, then the $cog elements disappear (I'm guessing due to timing issues with the fadeOut animation)

Is there a way when calling a function against multiple DOM objects in a jQuery object to know when it's done with the last item?

© Stack Overflow or respective owner

Related posts about jQuery