1
$($('div').children()).animate({opacity:"0"},
                        function(){
                                $(this).remove();
                                $('div').append('new content');
                            }
                        );

New content is getting doubled every time, how would I go about making the callback function only fire once instead of for each child?

Alnitak
  • 334,560
  • 70
  • 407
  • 495
Larry
  • 1,231
  • 4
  • 12
  • 18
  • FYI, you don't have to pass `$('div').children()` to jQuery again, just chain the call: `$('div').children().animate(...)`. – Felix Kling Jun 29 '13 at 16:17
  • More duplicates: http://stackoverflow.com/q/909526/218196, http://stackoverflow.com/q/8790752/218196 – Felix Kling Jun 29 '13 at 16:19

1 Answers1

1

Use .promise along with .done:

From the documentation:

The .promise() method returns a dynamically generated Promise that is resolved once all actions of a certain type bound to the collection, queued or not, have ended.

By default, type is "fx", which means the returned Promise is resolved when all animations of the selected elements have completed.

So the usage would be:

$(myElements).animate(...).promise().done(function() {
    // called only when _all_ of the animations on
    // "myElements" elements have completed
});
Community
  • 1
  • 1
Alnitak
  • 334,560
  • 70
  • 407
  • 495