Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add pause between each iteration of jQuery .each()?

I'm grabbing an array of jQuery objects and then via .each() modifying each individual jquery with in the array.

In this case I'm updated the class names to trigger a -webkit-transition-property to utilize a css transition.

I'd like there to be a pause before each css transition begins. I'm using the following, but there is no delay in between each update. Instead, they all appear to be updating at once.

function positionCards() {   $cards = $('#gameboard .card');   $cards.each(function() {       setTimeout( function(){ addPositioningClass($(this)); }, 500 )   }); }  function addPositioningClasses($card){   $card     .addClass('position') } 

I was hoping setTimeout would solve this, but it doesn't seem to be working. Is there a way to accomplish the pause before each CLASS name update of each object?

like image 215
DA. Avatar asked Mar 05 '11 07:03

DA.


People also ask

How do you pause a forEach loop?

Now you can use delayLoop() on any forEach() loop. Simply pass the function to use and the amount of time — in milliseconds — to delay between each iteration.

How do you continue a loop in jQuery?

each() loop and a $. each() loop at a particular iteration by making the callback function return false . Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration. Note that $(selector).


Video Answer


2 Answers

I added this as a comment but now that I have read it correctly and answered my own question this would probably work:

function positionCards() {   var $cards = $('#gameboard .card');    var time = 500;    $cards.each(function() {       setTimeout( function(){ addPositioningClass($(this)); }, time)       time += 500;   }); } 
like image 184
Rob Avatar answered Sep 21 '22 13:09

Rob


Sorry for digging up an old thread, but this tip could be useful for similar issues:

$cards.each(function(index) {     $(this).delay(500*index).addClass('position'); }); 
like image 25
johnjohn Avatar answered Sep 21 '22 13:09

johnjohn