Creates a new instance of the Timer class. Initializes the timer by setting the start time to the current timestamp.
Private#elapsedPrivate#startPrivate#stopGets the elapsed time in milliseconds. If the timer is running, returns the currently elapsed time; otherwise, returns the time elapsed between the start and stop points.
The elapsed time in milliseconds.
Resets the timer to its initial state. Sets the start time to now, and clears the stop time and elapsed time.
This timer instance, allowing for method chaining
Stops the timer if it's running.
If the timer is already stopped, this method has no effect. When stopped, the elapsed time is updated by adding the time since the timer was started.
The timer instance for method chaining
Returns a string representation of the elapsed time.
Optionalformat: "ms" | "seconds" | "minutes"The time format to use for the output. - 'ms': milliseconds (default) - 'seconds': seconds with one decimal place - 'minutes': minutes in MM:SS format
A formatted string representing the elapsed time.
Utility class for measuring and tracking elapsed time.
The Timer class provides functionality to measure elapsed time with millisecond precision. It can be started, stopped, and reset, and supports different formatting options when converting to a string representation.
Example
The Timer instance can be used directly in string contexts or arithmetic operations through its implementation of Symbol.toPrimitive.