.repeat(interval ,runNow ,callback)
- interval –
- the number of milliseconds between each trigger.
- runNow –
- whether to start the loop immediately for the first time.
- Defaults to
- callback –
- 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
Examples with the interval loop
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.⇒
See also another example for the same task using iterated timeouts instead.
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($).⇒
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.
The whole method chain
is repeated once per second.
In this code the object
Date is used as a callback function to get a simple example for changing 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().⇒
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,
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.
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.⇒
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:⇒