Timer class provides a comprehensive set of methods for controlling playback, seeking, and managing the timer’s lifecycle.
Playback Control
play()
Starts or resumes playback in the forward direction. If the timer is reversed, it will alternate to forward playback.Timer - The timer instance for chaining
Example:
pause()
Pauses the timer. Triggers theonPause callback.
Timer - The timer instance for chaining
Example:
resume()
Resumes playback without changing the direction. Does nothing if already playing.Timer - The timer instance for chaining
Example:
restart()
Resets the timer to the beginning and resumes playback.Timer - The timer instance for chaining
Example:
reverse()
Starts or resumes playback in reverse. If not already reversed, it will alternate to reverse playback.Timer - The timer instance for chaining
Example:
alternate()
Alternates the playback direction based on the current state andalternate parameter.
Timer - The timer instance for chaining
Example:
Seeking
seek()
Seeks the timer to a specific time position.Target time position in milliseconds
Whether to suppress callbacks during the seek (0 = false, 1 = true)
Internal flag for rendering behavior (0 = false, 1 = true)
Timer - The timer instance for chaining
Example:
State Management
reset()
Resets the timer to its initial state without playing.If true, performs a soft reset preserving certain internal states
Timer - The timer instance for chaining
Example:
init()
Initializes the timer and optionally starts autoplay. This should be called after creating a timer.Internal flag for rendering behavior
Timer - The timer instance for chaining
Example:
cancel()
Cancels the timer and pauses it. For timers with children, cancels all children. For animations, removes tween siblings.Timer - The timer instance for chaining
Example:
revert()
Seeks back to time 0, reverts any attached scroll observer, and cancels the timer.Timer - The timer instance for chaining
Example:
complete()
Immediately completes the timer by seeking to its duration, triggering theonComplete callback, and cancelling it.
Whether to suppress the onComplete callback (0 = false, 1 = true)
Timer - The timer instance for chaining
Example:
Timing
resetTime()
Resets the internal timing calculations. Automatically called when changing speed.Timer - The timer instance for chaining
Example:
stretch()
Changes the duration of the timer and scales all time-related properties proportionally.New total duration in milliseconds
Timer - The timer instance for chaining
Example:
Promises
then()
Returns a Promise that resolves when the timer completes. Useful for sequencing operations.Optional callback function to execute when the promise resolves
Promise<Timer> - Promise that resolves with the timer instance
Example:
Factory Function
createTimer()
Helper function to create and initialize a timer in one step.Same parameters as the Timer constructor
Timer - An initialized timer instance
Example: