I have a code like this:
for(i=0;i<somevalue;i++){
$(".elem[order='"+i+"']").delay(i*duration).animate({opacity:0},duration);
}
and at when the last item animation completes, I want to execute
$(“.elem”).remove();
I’m currently doing this with
$(".elem[order='"+i+"']").animate({opacity:0},duration,
function(){
if(i==somevalue-1){
$(".elem").remove();
}
});
But how can I do this with $.Deferred? I’m learning jQuery Deferred’s and I couldn’t solve this.
UPDATE 2:
Here is a working example using $.Deferred:
UPDATE 1, BASED ON NEWLY INTRODUCED DELAY
To answer your comment below, I think your best bet is to simply fire off the callback on the animation that fires last. Since you’re in full control of deciding duration (i.e. it’s not a randomly generated duration), then you could compute what the last duration will be and use that to decide if it will be the correct iteration in which to fire the callback:
ORIGINAL ANSWER
It looks like you want all of these elements to start animating at the same time, and have the same duration. Based on this, I would suggest doing a single animate call, and modifying your approach to gathering the collection of elements.
Example:
I haven’t actually ran this code, but I’m pretty confident it should work.