Runs a callback on each element of a jQuery collection with a timer in between
Use it like this:
$( ".selector" ).sequence( callback, 500 );
This will apply your callback
function on each element of the collection with a delay of 500ms between each call.
You can find a small interactive playground here.
.sequence()
takes 3 arguments:
The callback function to run over each element of the given collection. The this
argument of the call will be the current element, just like it is in jQuery.each
.
The interval between each call in milliseconds.
An optional boolean, defaulting to true
. Tells if the sequence should run as it is created or be hold()
(see below) immediately to wait for further commands.
Calling .sequence( [...] )
returns a sequence controller object (called ctrl
below).
It contains a $.Deferred()
promise as the ctrl.promise
property, so you can run the familiar jQuery promise-like methods to determine when the sequence finishes.
Additionally, there are the following methods appended:
Pauses the execution of the sequence instantly.
Unpauses the execution of the sequence.
The when
parameter can either be
"now"
to execute the next call on the sequence immediately (default)"delayed"
to wait a full turn and then run the callback or"remaining"
to resume exactly at the point where the execution was put on hold.
Runs all remaining calls immediately.
The ctrl.promise
will be resolved.
Puts the execution on hold and clears the sequence queue. The ctrl.promise
will be rejected.
Resets the sequence and removes all promise handlers. Be aware that the changes made by the previous sequence calls naturally can't be reverted by this.