Timer

View source

Stores mutable time and cycle state for an application timer.

Each update advances the timer while it is active, and its cycle behavior determines whether time stops at the end of the first cycle or wraps into subsequent cycles. Pausing preserves the current position; restarting begins again from zero.

Examples

import { Timer, TimerMode } from '@wimaengine/time'

const timer = new Timer({ duration: 2, mode: TimerMode.Once })

timer.update(0.5)
timer.pause()
timer.play()
timer.update(1.5)

if (timer.completed()) {
  console.log('timer finished')
}

Constructor


constructor(options?: TimerOptions)

View source

Creates a timer with an optional duration, mode, speed, and paused state.

Parameters

NameDescriptionDefault
options?—{}

Properties


paused: boolean

View source

Whether updates currently leave the timer’s elapsed time unchanged.

Methods


staticserialize(value: Timer): object

View source

Parameters

NameDescriptionDefault
value——

elapsed(): number

View source

Returns the elapsed time in the current cycle, in seconds.

This reads the current value without changing the timer. In TimerMode.Once mode it reaches duration when the timer finishes; in TimerMode.Repeat mode it wraps when a cycle ends.

Examples

timer.update(0.25)
console.log(timer.elapsed()) // 0.25

Returns

Elapsed seconds in the current cycle.

progress(): number

View source

Returns elapsed time as a ratio of the configured cycle duration.

This is the raw division of Timer#elapsed by Timer#duration; it does not mutate the timer or clamp the result. A zero duration therefore produces NaN for zero elapsed time or an infinite result for non-zero elapsed time.

Returns

Elapsed-to-duration ratio, where 1 represents one full cycle.

play(): void

View source

Resumes progression without resetting the current elapsed time.

pause(): void

View source

Pauses progression without resetting the current elapsed time.

start(): void

View source

Restarts the timer from the beginning and resumes progression.

This clears the completed-cycle count and elapsed seconds, then delegates to Timer#play; the playback-change request is therefore resolved by the next Timer#update.

stop(): void

View source

Stops the timer and resets its cycle state.

This clears the completed-cycle count and elapsed seconds, then delegates to Timer#pause; the timer is paused and its finished state is cleared through the resulting playback request.

reset(): void

View source

Resets the timer by delegating to Timer#start.

The timer therefore resumes from zero, clears its completed-cycle count, and requests a playback change.

seek(timestamp: number): void

View source

Parameters

NameDescriptionDefault
timestamp——

requestPlayback(): void

View source

Requests playback state resolution on the next timer update.

The request flag is set and the finished state is cleared. This method does not change Timer#paused or elapsed time; callers such as Timer#play, Timer#pause, and Timer#seek control those values separately.

playbackChanged(): boolean

View source

Reports whether the most recent timer update resolved a playback request.

The result is true only during the update in which a pending request was consumed; the next Timer#update clears the resolved flag. Calling this method does not mutate the timer.

Returns

Whether the latest update resolved a playback request.

completed(): boolean

View source

Reports whether a one-shot timer has reached its duration.

Repeating timers continue cycling and therefore do not report completion.

Returns

true after a Once timer finishes; otherwise false.