constructor(options?: TimerOptions)
View sourceCreates a timer with an optional duration, mode, speed, and paused state.
Class
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.
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(options?: TimerOptions)Creates a timer with an optional duration, mode, speed, and paused state.
Timer behavior for a single cycle.
Length of one timer cycle in seconds.
Multiplier applied to each delta passed to Timer#update.
Whether updates currently leave the timer’s elapsed time unchanged.
copy(source: Timer, target: Timer): Timerclone(target: Timer): Timerdefault(): Timerserialize(value: Timer): objectdeserialize(value: TimerSerial, out?: Timer): TimervalidateSerial(value: TimerSerial): value is TimerSerialelapsed(): numberReturns 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.
timer.update(0.25)
console.log(timer.elapsed()) // 0.25progress(): numberReturns 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.
play(): voidResumes progression without resetting the current elapsed time.
pause(): voidPauses progression without resetting the current elapsed time.
start(): voidRestarts 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(): voidStops 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(): voidResets 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): voidrequestPlayback(): voidRequests 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(): booleanReports 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.
update(dt: number): voidAdvances the timer by a frame delta in seconds.
The delta is multiplied by Timer#speed. Paused or finished
timers do not advance. A repeating timer can complete multiple cycles in
one update; use Timer#cyclesCompletedThisFrame or
Timer#cycleEnded to inspect that update.
cycleStarted(): booleancycleEnded(): booleancyclesCompleted(): numbercyclesCompletedThisFrame(): numbercompleted(): booleanReports whether a one-shot timer has reached its duration.
Repeating timers continue cycling and therefore do not report completion.