Description: Begin a sequential loop running for all selected elements – one after the other.

$ – the jQuery token
just indicates that the elements should be processed in a row.

In the instant case this is comparable to the classical usage with callback function.

Usage pattern


Examples with the sequential each-loop

Example #6: iterate all selected elements with a timeout

Here we combine two loops. The outer loop is an open interval loop repeating once per second. The inner loop is a sequential each-loop toggling the CSS class for every selected span element with a timeout after each step.

$('.example span').repeat(1000)

This is a sample sentence with some words.

Example #12: cyclic button clicks

The buttons can be clicked one after the other. At the end the cycle starts over and over.

Remark that the code is the same no matter how many buttons we have.

$('.example button').repeat().each($)

Example #17: scheduling different elements' animations

Here we use an instant repeat-loop together with a sequential each-loop.

In each iteration step we wait until the element is completely shifted up using .animate({opacity:0,top:-50},$)​. Then the next animation is queued with .animate({opacity:1,top:0}), which shifts the element down again but without waiting. The loop starts its next iteration, now using the next element. So we have one element going up and another element going down simultaneously.

As you see the usage of $ as token in place of the animate callback handler decides about whether to wait for the animation to finish.

$('.example span').repeat().each($)

Example #18: falling little boxes

This example first creates 25 little boxes with an inline definition:

These 25 boxes shall be animated as falling down with each button click.

A helper function is necessary to animate them in reverse order:
$.fn.reverse = function() {
  return this.$(this.get().reverse());

Try clicking on that button multiple times during animation. With .stop(true,true) and .unwait() all animations are reset at the beginning.

  .$('#boxparent div').stop(true,true).unwait()

Example #19: pause animation cycle on mouseover

There is a animation cycle rotating three images. Once you move the mouse cursor over it, it pauses until the mouse is moved away.

A little helper function checks whether the mouse is over the image. If that's the case a deferred jQuery object waiting for the mouseleave event is returned.
function noHover() {
    return this.is(':hover') ? this.wait('mouseleave') : this

The running loop can be written in a single jQuery line.

$('#images > img').hide().repeat().each($).fadeIn($)