notatimer is "not a timer." It is, in fact, a stopwatch, though it doesn't stop some people (see: speedrunners) from calling it a timer.
npm i notatimer
// CommonJS
const Stopwatch = require('notatimer');
// ESM
import Stopwatch from 'notawatch';
It is a default export, so it can be named whatever you want.
const stopwatch = new Stopwatch()
Optionally, an object can be passed into the constructor, with three possible properties.
const stopwatch = new Stopwatch({
initial: 0, // must be a number
delay: 0, // must be a nonnegative number
callback: null // must be a function with one parameter -- details below
})
The callback function is passed an object containing a time
property and times
property.
These three properties can be updated after construction using stopwatch.set(object)
.
Current time in milliseconds (accurate to 5 microseconds).
Current time as an array in the format [hours, minutes, seconds, milliseconds]
.
If the stopwatch has started (true/false).
If the stopwatch is currently running (true/false).
If the stopwatch has finished (true/false).
The initial time for the stopwatch (use the constructor or stopwatch.set()
to set).
The initial delay for the stopwatch (use the constructor or stopwatch.set()
to set).
Array of objects, each with a time
property and times
property.
Function that is called every step or when setting
a property. Use this function to update where the time is displayed.
Start the timer. Returns void.
Pause the timer. Returns void.
Stops the timer. Returns an object with a time
property and times
property.
Stops and resets the timer to the initial value. Returns void.
Adds an object with a time
property and times
property to thestopwatch.laps
array. Returns the aforementioned object.
Sets the initial
property, delay
property, and/or callback
function. Returns void.