Initializes a new instance of the BasicTimer class, optionally auto-starting it.
Determines whether the timer will immediately start; default is false.
Protected field with the array of partial lap time values.
Protected field with the time value representing the moment the lap started.
Protected field with the time value representing the moment the pausing started.
Protected field with the time value representing the sum of all previous pause times.
Protected field with the time value representing the moment the timer started.
Gets the time elapsed since the start of the current lap until now.
Gets the time representing the moment the current lap started.
Gets whether the timer is paused.
This property is not influenced by pauses, i.e. it will return true even when paused.
Gets the number of laps currently stored at the stopwatch.
Gets the name used on stopwatch errors.
Finishes the current lap and starts a new one. This will add a new entry to partial times and start counting time for the new lap.
Note: The first lap always start in parallel with the stopwatch.
The partial time of the finished lap (elapsed time from the start of the lap until now).
Protected alias to elapsedTime.
Protected alias to isRunning.
Prevents the object from counting the time until it is resumed.
Both the elapsed time and the time resulting from stop won't change during the pause, but pausedTime and totalPauseTime will.
Begins to count time, setting the start time to a value equivalent to now. This also starts the first lap of the stopwatch.
Time that represents now (or better yet, the moment the stopwatch has started).
Ends the time counting, returning the total elapsed time (sum of all lap partials) and resetting the object to its default state. This will automatically end the last created lap!
If the stopwatch is paused, there is no need to resume before calling this method.
A closure that can be used to retrieve additional data about the stopwatch. This information would otherwise be lost after the method is completed.
A time with the total amount of nanoseconds spent between start and now.
Generated using TypeDoc
Counts the duration of runtime operations, with pausing capabilities and time segmentation — which are arbitry divisions of time.
An instance of StopWatch is able to count the duration between its start and stop, pausing, which temporarily stops the time counting, and create laps, which splits the stopwatch . Every start-stop cycle resets the object to its default state.
See BasicTimer and Timer for simpler timer implentations (less overhead).
Examples
Using a single StopWatch to perform consecutive countings
import { StopWatch } from "timecount/utils"; const stopwatch = new StopWatch(); while (thereAreThingsToDo) { stopwatch.start(); doTheThings(); stopwatch.stop().to("second"); // 1.2021 s }
Using a TimeWriter to write StopWatch results
import { TimeWriter } from "timecount"; const stopwatch = new StopWatch(); const timeWriter = new TimeWriter({ verbose: true }); stopwatch.start(); doSomething(); const time = timer.stop(); timeWriter.write(time, "millisecond"); // 10156.663207 milliseconds timeWriter.write(time, "second"); // 10.156663207 seconds timeWriter.write(time, "minute"); // 0.169277720116666668 minute
Pausing and resuming the stopwatch
const stopwatch = new StopWatch(true); thingsYouWantMeasured(); // Let's say this takes 5 seconds stopwatch.pause(); thingsYouDontWantMeasured(); // And this another 5 seconds stopwatch.stop().to("second"); // 5 s (paused time is not counted)
Creating time categories according to the stopwatch laps
import { TimeWriter } from "timecount"; import { StopWatch } from "timecount/utils"; const stopWatch = new StopWatch(true); const timeWriter = new TimeWriter({ verbose: true }); doCategory1(); "Category 1: " + timeWriter.write(stopWatch.endLap(), "second"); // Category 1: 1.454 seconds doCategory2(); console.log("Category 2 - ", timeWriter.write(stopWatch.endLap(), "second")); // Category 2: 3.092017 seconds stopWatch.pause() unimportantThing(); console.log("TOTAL - ", timeWriter.write(stopWatch.stop(), "second")); // Result: Category 1 - 4.589632 seconds