tags:

views:

53

answers:

3

I am expecting when I go

$.each($(something).find(something), function(){
   $(this).delay(1000).fadeOut();
});

then for each matching element I get a second of delay before it is gone. but what I get is a second of delay and then it all fades out. its 3am and I can't think. please help

+1  A: 

If I'm interpreting your question right, you want things to fade out over the duration of a second? If so, what you want is $(this).fadeOut(1000);, which sets the duration of a fade; doing the delay(1000) just waits a second before it starts your fadeOut() action.

Tim
Also, this doesn't need to be iterated: this should work `$(something).find(somethingelse).fadeOut(1000)` -- do it all at once (no `each` required)
Michael Haren
what i want is delay 1sec -> elem[0] fades out -> delay 1 sec -> elem[1] fades out.
XGreen
now thinking about it each fade out function could be a call back for the fadeout of the prev element but probably it will look like the worst code ever written in history
XGreen
In that case, you want to run animations *sequentially*. refer to this http://stackoverflow.com/questions/1218152/how-can-i-animate-multiple-elements-sequentially-using-jquery
Michael Haren
that is not very nice is it? I mean I am dealing with unknown number of elements here and they could be a lot
XGreen
Yeah, you might be looking at making a new plugin for something like this (or searching for one)
Michael Haren
+1  A: 

This should be the basic idea:

var set = $(something).find(something);
var delayFade = function(){
     $(this).delay(1000).fadeOut(400, nextDelayFade);
};
var i = 0;
var nextDelayFade = function()
{
  if(i < set.length)
  {
    $(set[i++]).each(delayFade);
  }
};
nextDelayFade();
Matthew Flaschen
+3  A: 

This will work, call it with a jQuery object as a parameter:

function fadeAll(elems) {
    var i=-1;
    function next() {
        i = i+1;
        if (i < elems.length)
            $(elems[i]).delay(1000).fadeOut(next);
    }
    next();
}

You can see it at work here.

interjay
This is nicely done mate. thanks
XGreen
how would you use this with animate instead of fadeOut? as if you go for example animate({something : 'its value'}, next());it jumps again right to the end
XGreen
@XGreen: That should say `next` instead of `next()`.
interjay