.repeat(callback)
- callback –
function(count…)
, optional - a method that is called once per loop iteration.
- All current iteration counts/events (from inner-most to outer-most loop) are passed as arguments.
The beginning context for each loop iteration depends on the usage of .until()
.
Usage pattern
Examples with the instant loop
Example #2: repeated blinking with iterated timeouts
By using .toggleClass() the CSS class switches from being added and removed after every timeout.
In this example a repeated timeout of 700 milliseconds is used.
⇒This is a blinking text.
See also another example for the same task using an interval loop instead.
Example #5: create dynamic tables with instant loops
Imagine you want to add very many table rows automatically so you can fill them with dynamic content.
E.g. a 30 x 200 sized table. Then you need only one callback method creating each cell.
The rest is done via a single jQuery chain:
⇒
Example #8: simple progress indicator with text
The text dots will go on and on in this example. Of course in a real use case it would be stopped using .unrepeat().
⇒Still processing
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 #16: playing pong with divs
In this example we work with two different animation queues.
The horizontal animation for the ball uses the effects queue named 'myFX'. We have to start that queue manually using .dequeue('myFX').
The vertical animation uses the default queue which always starts on its own. It includes all three moving elements at once.
Because the two queues will be animated with different repeating intervals the ball doesn't always take the same path.
⇒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 #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:Try clicking on that button multiple times during animation. With .stop(true,true) and .unwait() all animations are reset at the beginning.
⇒ 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.The running loop can be written in a single jQuery line.
⇒Example #20: ruler with animation
This example shows how to create a ruler with an animated selection mark.
Each time the user clicks on one of the tick marks the button moves up to this mark. During its animation all underlying ticks get highlighted when the button flies by.
This is the complete code:
⇒