Discovering deferred objects in jQuery

April 3, 2012  |  No Comments
featuredimage_javascript

Using callbacks in animations is very easy, but what happens if you wish to use one callbacks after animating several elements? Something like this:

$(".myclass").fadeIn(function(){
  //This callback may be executed several times!!!
});

If you apply a callback directly here, you will execute it as many times as elements are being animated. The solution is something called deferred objects and its used like this:

$.when(
    $('myClass').fadeIn();
)
.done(function() {
    //This will be executed only ONCE, after all the animations are over
});


Leave a Reply

Your email address will not be published. Required fields are marked *