Class
Clock
View sourceTracks elapsed time and the duration of the most recent update.
Supply timestamps to Clock#update to record a duration in seconds
and accumulate elapsed seconds. Use Clock#start to reset the
accumulated values when a new timing interval begins. For a shared
application clock, use VirtualClock.
Examples
import { Clock } from '@wimaengine/time'
const clock = new Clock()
clock.update(1000)
console.log(clock.getDelta()) // 1 second
clock.update(1016)
console.log(clock.getDelta()) // 0.016 seconds
console.log(clock.getElapsed()) // 1.016 seconds
console.log(clock.getFrameRate()) // 62.5 frames per second
clock.start()
console.log(clock.getElapsed()) // 0Constructor
Methods
start(): void
View sourceResets the elapsed time and current frame delta to zero.
The last update timestamp is preserved. The next call to
Clock#update therefore measures from the timestamp before the
reset.
getElapsed(): number
View sourceReturns the total time accumulated by the clock, in seconds.
getDelta(): number
View sourceReturns the duration calculated by the most recent update, in seconds.
This is 0 before the first update and after Clock#start.
getFrameRate(): number
View sourceReturns the instantaneous frame rate derived from the current delta.
The value is calculated as 1 / delta; it is not averaged or smoothed.
When the delta is 0, JavaScript returns Infinity.
update(accumulate?: number): number
View sourceAdvances the clock using an absolute timestamp.
The timestamp is expected in milliseconds, matching performance.now().
The resulting delta and the accumulated elapsed time are in seconds.
With no argument, the method uses performance.now().
staticstart(clock: Clock): void
View sourceResets a clock’s elapsed time and current frame delta to zero.
lastTick is intentionally left unchanged, so the next update measures
from the timestamp that was recorded before the reset.
staticgetElapsed(clock: Clock): number
View sourceGets the total elapsed time of a clock, in seconds.
staticgetDelta(clock: Clock): number
View sourceGets the duration calculated by the most recent update, in seconds.
staticgetFrameRate(clock: Clock): number
View sourceGets the instantaneous frame rate derived from the current delta.
The result is 1 / clock.delta; a zero delta therefore produces
JavaScript’s Infinity rather than a finite or averaged frame rate.
staticupdate(clock: Clock, accumulate?: number): number
View sourceAdvances a clock using an absolute timestamp in milliseconds.
The difference between the supplied timestamp and the previous
lastTick is divided by 1000 to produce a delta in seconds. That delta
is added to elapsed, then the supplied timestamp is stored as
lastTick. When omitted, the timestamp defaults to performance.now().
staticserialize(value: Clock): ClockSerial
View sourceConverts a clock into the plain state used by the type registry.
The returned object contains the elapsed time and delta in seconds, plus the most recent timestamp in milliseconds.
staticdeserialize(value: ClockSerial, out?: Clock): Clock
View sourceRestores clock state from a serialized value.
When out is supplied, it is updated and returned. Otherwise a new
Clock is created. Deserialization restores elapsed, lastTick, and
delta exactly; it does not advance the clock or read the current time.
