Skip to main content

Class: Timer

A class to create, start, and stop named timers that measure elapsed time in milliseconds.

Constructors

new Timer()

private new Timer(name): Timer

Parameters

name: string

Returns

Timer

Source

Timer.ts:19

Properties

cumulativeElapsed

private cumulativeElapsed: number = NaN

cumulativeElapsed is a cumulative total of elapsed time while the timer was in previous started (running) states, NOT INCLUDING the possibly active run's duration

Source

Timer.ts:14


name

private name: string

Source

Timer.ts:15


startTime

private startTime: number = NaN

Source

Timer.ts:6


stopTime

private stopTime: number = NaN

Source

Timer.ts:7


stopped

private stopped: boolean = true

Source

Timer.ts:8


_timers

static private _timers: Timer[]

Source

Timer.ts:17

Methods

elapsed()

static elapsed(name): number

Returns the total time elapsed, in milliseconds, of the timer.

Parameters

name: string

The name of the timer whose elapsed duration is requested

Returns

number

Remarks

The total time elapsed will include all durations from multiple starts and stops of the timer, if applicable. A timer's elapsed duration can be read while it is in started or stopped state. The method throws an error if a timer with the given name does not exist.

Source

Timer.ts:134


exists()

static exists(name): boolean

Checks if a timer of the given name exists.

Parameters

name: string

The name of the timer to check for existence

Returns

boolean

boolean

Remarks

The method checks if there is a timer with the given name.

Source

Timer.ts:191


new()

static new(name): void

Creates, but does not start, a new millisecond-resolution timer based on performance.now().

Parameters

name: string

The name of the timer to be started

Returns

void

Remarks

If a timer with the given name already exists, it will be created and set back to zero, but not started.

Source

Timer.ts:47


now()

static now(): number

Aliases performance.now()

Returns

number

a DOMHighResTimeStamp

Remarks

The m2c2kit Timer class is designed to measure elapsed durations after a designated start point for a uniquely named timer. However, if a timestamp based on the time origin is needed, this method can be used.

Source

Timer.ts:34


remove()

static remove(name): void

Removes a timer.

Parameters

name: string

The name of the timer to be removed

Returns

void

Remarks

After removal, no additional methods can be used with a timer of the given name, other than to create a new timer with the given name, whose duration will be set at 0 again. The method throws an error if a timer with the given name does not exist.

Source

Timer.ts:160


removeAll()

static removeAll(): void

Remove all timers.

Returns

void

Remarks

This method will remove any timers in a started or stopped state. This method is idempotent; method is safe to call even if there are no timers to remove; no errors are thrown if there are not any timers that can be removed.

Source

Timer.ts:179


start()

static start(name): void

Starts a stopped millisecond-resolution timer based on performance.now().

Parameters

name: string

The name of the timer to be started

Returns

void

Remarks

The method throws an error if a timer with the given name does not exist or is not in a stopped state.

Source

Timer.ts:77


startNew()

static startNew(name): void

Creates and starts a new millisecond-resolution timer based on performance.now().

Parameters

name: string

The name of the timer to be started

Returns

void

Remarks

If a timer with the given name already exists, it will be created, set back to zero, and started.

Source

Timer.ts:63


stop()

static stop(name): void

Stops a timer.

Parameters

name: string

The name of the timer to be stopped

Returns

void

Remarks

The method throws an error if a timer with the given name is already in a stopped state, or if a timer with the given name has not been started.

Source

Timer.ts:104