When animating in jQuery, what's best practice for firing a callback only when ALL elements are done animating and not for each element?
For example:
$('.someElements').fadeOut('fast', function() {
// dont do this until ALL elements are done fading
}
This could be a snippet to try:
The alert Is just a placeholder to the end-callback you need to fire.
EDIT (another way):
You can also catch all the elements but not the last one.
and then add a fadeOut with callback only to It
This is a great question, as per the jQuery docs:
To work around this limitation you could:
.someElements
, and set up a separate callback for each one.count
variable that keeps track of how many total callbacks there are.count
until it reaches 0.When count reaches 0, all callbacks are complete, and you will be guaranteed that all elements are done animating. From here, you can then have special code in your callback that does whatever you need to do...
More recent releases of jQuery (version 1.6 and later) include the idea of a promise. Using this function eliminates the need for a workaround. For example: