Skip to main content
The 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.play()
Returns: Timer - The timer instance for chaining Example:
const timer = new Timer({ duration: 2000 });
timer.init();
timer.pause();
timer.play(); // Resume playback

pause()

Pauses the timer. Triggers the onPause callback.
timer.pause()
Returns: Timer - The timer instance for chaining Example:
timer.pause();
console.log(timer.paused); // true

resume()

Resumes playback without changing the direction. Does nothing if already playing.
timer.resume()
Returns: Timer - The timer instance for chaining Example:
timer.pause();
// ... later
timer.resume(); // Continue from where it paused

restart()

Resets the timer to the beginning and resumes playback.
timer.restart()
Returns: Timer - The timer instance for chaining Example:
timer.restart(); // Start from the beginning

reverse()

Starts or resumes playback in reverse. If not already reversed, it will alternate to reverse playback.
timer.reverse()
Returns: Timer - The timer instance for chaining Example:
timer.reverse(); // Play backwards

alternate()

Alternates the playback direction based on the current state and alternate parameter.
timer.alternate()
Returns: Timer - The timer instance for chaining Example:
// Manually alternate direction
timer.alternate();

Seeking

seek()

Seeks the timer to a specific time position.
timer.seek(time, muteCallbacks, internalRender)
time
Number
required
Target time position in milliseconds
muteCallbacks
Boolean | Number
default:"0"
Whether to suppress callbacks during the seek (0 = false, 1 = true)
internalRender
Boolean | Number
default:"0"
Internal flag for rendering behavior (0 = false, 1 = true)
Returns: Timer - The timer instance for chaining Example:
// Seek to 1 second
timer.seek(1000);

// Seek without triggering callbacks
timer.seek(1500, 1);

// Seek using progress property
timer.progress = 0.5; // Seek to halfway

// Seek using currentTime property
timer.currentTime = 2000;

State Management

reset()

Resets the timer to its initial state without playing.
timer.reset(softReset)
softReset
Boolean
default:"false"
If true, performs a soft reset preserving certain internal states
Returns: Timer - The timer instance for chaining Example:
timer.reset(); // Reset to beginning, timer is paused
timer.reset(true); // Soft reset

init()

Initializes the timer and optionally starts autoplay. This should be called after creating a timer.
timer.init(internalRender)
internalRender
Boolean
default:"false"
Internal flag for rendering behavior
Returns: Timer - The timer instance for chaining Example:
const timer = new Timer({ duration: 2000, autoplay: true });
timer.init(); // Initialize and auto-start

cancel()

Cancels the timer and pauses it. For timers with children, cancels all children. For animations, removes tween siblings.
timer.cancel()
Returns: Timer - The timer instance for chaining Example:
timer.cancel();
console.log(timer.cancelled); // true

revert()

Seeks back to time 0, reverts any attached scroll observer, and cancels the timer.
timer.revert()
Returns: Timer - The timer instance for chaining Example:
timer.revert(); // Reset to start and cancel

complete()

Immediately completes the timer by seeking to its duration, triggering the onComplete callback, and cancelling it.
timer.complete(muteCallbacks)
muteCallbacks
Boolean | Number
default:"0"
Whether to suppress the onComplete callback (0 = false, 1 = true)
Returns: Timer - The timer instance for chaining Example:
timer.complete(); // Jump to end and complete
timer.complete(1); // Complete without triggering onComplete

Timing

resetTime()

Resets the internal timing calculations. Automatically called when changing speed.
timer.resetTime()
Returns: Timer - The timer instance for chaining Example:
timer.resetTime(); // Recalculate timing

stretch()

Changes the duration of the timer and scales all time-related properties proportionally.
timer.stretch(newDuration)
newDuration
Number
required
New total duration in milliseconds
Returns: Timer - The timer instance for chaining Example:
const timer = new Timer({ duration: 1000, delay: 500 });
timer.stretch(2000); // Duration becomes 2000ms, delay becomes 1000ms

Promises

then()

Returns a Promise that resolves when the timer completes. Useful for sequencing operations.
timer.then(callback)
callback
Callback<Timer>
Optional callback function to execute when the promise resolves
Returns: Promise<Timer> - Promise that resolves with the timer instance Example:
// Using async/await
async function animate() {
  const timer = new Timer({ duration: 1000 }).init();
  await timer;
  console.log('Timer completed!');
}

// Using .then()
timer.then((timer) => {
  console.log('Done!');
  return new Timer({ duration: 500 }).init();
}).then((timer2) => {
  console.log('Second timer done!');
});

// Chaining animations
await timer1;
await timer2;
console.log('All done!');

Factory Function

createTimer()

Helper function to create and initialize a timer in one step.
import { createTimer } from 'animejs';

const timer = createTimer(parameters)
parameters
TimerParams
Same parameters as the Timer constructor
Returns: Timer - An initialized timer instance Example:
import { createTimer } from 'animejs';

const timer = createTimer({
  duration: 2000,
  onComplete: () => console.log('Done!')
});

// Equivalent to:
const timer2 = new Timer({
  duration: 2000,
  onComplete: () => console.log('Done!')
}).init();

Method Chaining

Most methods return the timer instance, enabling method chaining:
timer
  .pause()
  .seek(1000)
  .play();

// Reset and restart with new speed
timer
  .reset()
  .stretch(2000)
  .play();

Build docs developers (and LLMs) love