.repeat(interval ,runNow ,callback)
- interval –
number
- the number of milliseconds between each trigger.
- runNow –
boolean
, optional - whether to start the loop immediately for the first time.
- Defaults to
false
. - 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 interval loop
Example #1: repeated blinking (like in the old days with the deprecated <blink>
element)
By using .toggleClass() the CSS class switches from being added and removed after every interval timeout.
In this example the interval timeout is set to 400 milliseconds.
⇒This is a blinking text.
See also another example for the same task using iterated timeouts instead.
Example #4: blinking a given number of times on click
We repeat .toggleClass() for an even number of times, so the class "blink" is absent again when the repeat loop finishes.
Remark that the click handler is assigned directly in the invocation chain using .click($).
⇒That text blinks two times after clicking .
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.
⇒This is a sample sentence with some words.
Example #7: clock display with one short blink per second
The whole method chain .text(Date).addClass('blink').wait(100).removeClass('blink')
is repeated once per second.
Here is a clock: --.
In this code the object Date
is used as a callback function to get a simple example for changing text.
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 #9: generating password with fake progress
Of course generating a random password is done in less than a millisecond. But you want to simulate heavy computational things going on – just for a nice user experience.
Given that you have a method which adds one random character at a time to an input value,e.g. ,
then you can use that to build our password generator.
⇒
So what happens here?
- First we interrupt any possibly ongoing password generation by using .unrepeat().
- We clear the current password value and add a nice animated gif in the background with .val('').addClass('load').
- Now we repeatedly add a new random character with .repeat(200).val(addRandomChar).
- After doing so for 10 times we stop and then remove our CSS class with .until(10).removeClass('load').
Try out clicking multiple times on that button. You will see, that the ongoing animation is always stopped before starting over.
Example #11: change the banner randomly in an interval loop
Given that you have a method which selects any element from the current jQuery set randomly,e.g. ,
then you can use that to build a banner rotator very easily.
In this example there are four different banners. Every 5 seconds another one fades in, randomly chosen.
As we don't want to wait for the animations finishing we use the original version of .fadeIn() and .fadeOut() here.
⇒Some banner text?
some more text here
Some other cool stuff?
this is other text
Even different text
this is new text
Attention!
cheaper, cheaper, cheaper…
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:
⇒