Stats is a utility object that provides a simple way of timing events and displaying statistics for debugging purposes. The timing functionality can also be used to easily calculate frame times when animating.
| Stats( ): Stats | Initializes with default parameters and will not show the stats to the user. |
|---|---|
| Stats( { show?: boolean, suspendOnBlur?: boolean } ): Stats | Initializes with the show option (default false) determining the stats visibility to the user and the suspendOnBlur option (default true) determining whether the timers are suspended when the page isn't active. |
| suspended: boolean | The state of suspension for the Stats object. If suspendOnBlur was set in the constructor, then this will be false when the window is not active and true when it is. |
|---|
| lastRead: number | The timestamp in milliseconds of the last time the timer was read. |
|---|---|
| show: boolean | Whether the stats will be added to the statBox or not on initialization. This is set in the constructor. |
| statBox: HTMLElement | The stat box element where all the stats are appended to. |
| stats: { [key: string]: { elem: HTMLElement, value: string | number } } | The object to store data for individual stats based on a key name. |
| timer: number | The timestamp in milliseconds of the start of the timer. |
| getStat(name: string): string | number | Returns the current value stored for the requested stat. If the stat doesn't exist, undefined is returned. |
|---|---|
| readCheckpoint(): number | Returns the time in milliseconds since the last readCheckpoint() call or the last readTimer() call. |
| readTimer(): number | Returns the time in milliseconds since the timer was started with startTimer(). |
| setStat(name: string, value: string | number, bias?: number, fractionDigits?: number): void | Sets the stat with the given name to the value specified. If the value type of the stat is a
number, it will be averaged with the new value according to the bias value. The bias is constrained to the range [0, 1]. The default value for the bias is 1, so only the given value will be considered. If the bias is less than 1, then the new value of the stat will be equal to bias * newValue + (1-bias) * oldValue. The final value will then be converted to a fixed-decimal value with the number of fraction digits equal to the given value. The default value of fractionDigits is 3. |
| startTimer(): void | Starts the timer at the call time. This uses the performance.now() function to store the current milliseconds. |
| addSleepListener(): void | Adds event listeners that will suspend the timer when the window blur event is triggered. The timers will reset when the focus event is triggered. |
|---|---|
| getStatBox(): void | Gets the stat box (id="statBox") from the DOM. If it doesn't exist, it is created and appended to the document body. |
Copyright © 2020 Trevor Richard