Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery each() with a delay

So, I would like an element to fade in and wait half a second, then fade the next in etc...

My code:

$('.comment').each(function() {                 
                    $(this).css({'opacity':0.0}).animate({
                        'opacity':1.0
                    }, 450).delay(500);
                });

I'm obviously doing something really silly.... (I hope)... My question is: Is this even possible? if not - can anyone point me in the right direction?

Thanking you!

like image 347
Barrie Reader Avatar asked Jun 15 '11 15:06

Barrie Reader


People also ask

How to add delay in jQuery function?

jQuery | delay() with ExamplesThe delay() is an inbuilt method in jQuery which is used to set a timer to delay the execution of the next item in the queue. para1: It specifies the speed of the delay. para2: It is optional and specifies the name of the queue.

How do you delay a function?

To delay a function call, use setTimeout() function. functionname − The function name for the function to be executed. milliseconds − The number of milliseconds. arg1, arg2, arg3 − These are the arguments passed to the function.

What is the use of the delay() method in jQuery?

The delay () is an inbuilt method in jQuery which is used to s et a timer to delay the execution of the next item in the queue. para1: It specifies the speed of the delay. para2: It is optional and specifies the name of the queue. Return Value: It returns the selected element with the specified speed. In the below code, timer is set ...

How to delay the execution of the next item in jQuery?

The delay() is an inbuilt method in jQuery which is used to set a timer to delay the execution of the next item in the queue. Syntax: $(selector).delay(para1, para2); Parameter: It accepts two parameters which are specified below-para1: It specifies the speed of the delay. para2: It is optional and specifies the name of the queue.

How do I delay between queued effects in jQuery?

The .delay () method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay— .delay () is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.

What is the syntax for the $selector delay function?

The syntax for this function is ‘ ($selector). delay (speed.name), where speed.name represents the delay value and name is an optional parameter used to title this function in the execution queue.


2 Answers

Or, something like this:

$.each($('.comment'), function(i, el){

    $(el).css({'opacity':0});

    setTimeout(function(){
       $(el).animate({
        'opacity':1.0
       }, 450);
    },500 + ( i * 500 ));

});

demo => http://jsfiddle.net/steweb/9uS56/

like image 162
stecb Avatar answered Oct 10 '22 19:10

stecb


try something like this

$(this).find('#results').children().each(function(index){
        $(this).delay(500 * index).slideDown(500);
})
like image 23
Ashkan Ghodrat Avatar answered Oct 10 '22 20:10

Ashkan Ghodrat