Clock Functions
A JavaScript/TypeScript library containing stopwatch and timer classes.
Highlights
- Supports TypeScript!
- Supports Node and browser
- Includes full JSX documentation
- Lightweight code (14kb!)
Installation
npm install clock-functions --save
Usage
const clockFn = require('clock-functions');
import clockFn from 'clock-functions';
Stopwatch
Stopwatch is a class used to measure time in milliseconds.
Constructor
Stopwatch constructor only has one optional parameter:
autoStart: boolean
- if the stopwatch should start automatically. Defaults to false
.
const stopwatch = new Stopwatch();
const stopwatch = new Stopwatch(true);
Methods
Stopwatch has the following methods:
start()
This method starts the stopwatch, if it is not already started.
If the stopwatch is already started, it does nothing.
start(emit: boolean): boolean
Returns false
if the stopwatch was already started, otherwise true
.
If emit
parameter is set to true
(default), the onstart
event will be emitted.
Example:
let stopwatch = new Stopwatch();
stopwatch.start();
stopwatch.start();
stop()
This method stops the stopwatch, if it is not already stopped.
If the stopwatch is already stopped, it does nothing.
stop(emit: boolean): boolean
Returns false
if the stopwatch was already stopped, otherwise true
.
If emit
parameter is set to true
(default), the onstop
event will be emitted.
Example:
let stopwatch = new Stopwatch();
stopwatch.stop();
stopwatch.start();
stopwatch.stop();
toggle()
This method toggles the stopwatch, depending on its current state.
toggle(emit: boolean): boolean
Returns true
if the stopwatch has been started, and false
if it has been stopped.
If emit
parameter is set to true
(default), the onstart
or onstop
event will be emitted, depending on the action taken.
Example:
let stopwatch = new Stopwatch();
stopwatch.toggle();
stopwatch.toggle();
stopwatch.toggle();
stopwatch.stop();
stopwatch.toggle();
isActive()
This method checks if the stopwatch is currently active, i.e. if it was started but wasn't recently stopped.
isActive(): boolean
Example:
let stopwatch = new Stopwatch();
stopwatch.isActive();
stopwatch.start();
stopwatch.isActive();
time()
and valueOf()
Those methods return the total time the stopwatch has been active for. They can be used interchangably.
time(emit: boolean): number
valueOf(emit: boolean): number
If emit
parameter is set to true
(default), the ontime
event will be emitted.
Example:
stopwatch = new Stopwatch();
stopwatch.start();
stopwatch.time();
stopwatch.stop();
stopwatch.time();
sinceStart()
This method returns the time since the last time the stopwatch has been started.
If the stopwatch was never started, it returns null
.
sinceStart(): number | null
Example:
let stopwatch = new Stopwatch();
stopwatch.start();
stopwatch.stop();
stopwatch.time();
stopwatch.start();
stopwatch.time();
stopwatch.sinceStart();
reset()
This method resets the stopwatch to 0ms, and keeps its current state.
If the stopwatch was never started, it does nothing.
reset(emit: boolean): boolean
Returns false
if the stopwatch was never started, otherwise true
.
If emit
parameter is set to true
(default), the onreset
event will be emitted.
Example:
let stopwatch = new Stopwatch();
stopwatch.start();
stopwatch.time();
stopwatch.reset();
stopwatch.time();
stopwatch.isActive();
cancelStart()
This method cancels the last start. The stopwatch stops, and the time remains as it was on the last stop.
If the stopwatch is stopped, it does nothing.
cancelStart(): boolean
Returns false
if the stopwatch was stopped, otherwise true
.
Example:
let stopwatch = new Stopwatch();
stopwatch.start();
stopwatch.stop();
stopwatch.start();
stopwatch.cancelStart();
stopwatch.cancelStart();
cancelStop()
This method cancels the last stop. The stopwatch starts, and the duration between the last stop and current time gets added to the total.
If the stopwatch is started, it does nothing.
cancelStop(): boolean
Returns false
if the stopwatch was started, otherwise true
.
Example:
let stopwatch = new Stopwatch();
stopwatch.start();
stopwatch.stop();
stopwatch.cancelStop();
stopwatch.cancelStop();
toString()
This method returns the same value as the time()
method, but with ms
string added at the end.
toString(): string
Example:
let stopwatch = new Stopwatch();
stopwatch.start();
stopwatch.toString();
Properties
Stopwatch has the following public properties:
onstart
This property should be a function, and can be freely changed.
It gets called if the onstart
event is emitted.
onstop
This property should be a function, and can be freely changed.
It gets called if the onstop
event is emitted.
onreset
This property should be a function, and can be freely changed.
It gets called if the onreset
event is emitted.
ontime
This property should be a function, and can be freely changed.
It gets called if the ontime
event is emitted.
Timer
Timer is a class used to delay the execution of a function.
Constructor
Timer constructor has three parameters:
delay: number
- the amount of time in ms until the function should be called.autoStart: boolean
- if the timer should start automatically. Optional, defaults to true
.callback: Function
- the function to be called. It can also be specified by setting the object's property onfinish
.
const timer = new Timer(10000);
const timer = new Timer(10000, false, () => console.log('Hello world!'));
const timer = new Timer(10000, false);
timer.onfinish = () => console.log('Hello world!');
Methods
Timer methods are extremely similar to Stopwatch methods, but there are no events emitted in any of the functions.
start()
This method starts the timer, if it is not already started.
If the timer is already started, it does nothing.
start(): boolean
Returns false
if the timer was already started, otherwise true
.
Example:
let timer = new Timer(10000);
timer.start();
timer.start();
stop()
This method stops the timer, if it is not already stopped.
If the timer is already stopped, it does nothing.
stop(): boolean
Returns false
if the timer was already stopped, otherwise true
.
Example:
let timer = new Timer(10000);
timer.stop();
timer.start();
timer.stop();
toggle()
This method toggles the timer, depending on its current state.
toggle(): boolean
Returns true
if the timer has been started, and false
if it has been stopped.
Example:
let timer = new Timer(10000);
timer.toggle();
timer.toggle();
timer.toggle();
timer.stop();
timer.toggle();
isActive()
This method checks if the timer is currently active, i.e. if it was started but wasn't recently stopped.
isActive(): boolean
Example:
let timer = new Timer(10000);
timer.isActive();
timer.start();
timer.isActive();
timeRemaining()
and valueOf()
Those methods return the time left until the function is called. They can be used interchangably.
time(): number
valueOf(): number
Example:
let timer = new Timer(10000);
timer.timeRemaining();
sinceStart()
This method returns the time since the last time the timer has been started.
If the stopwatch was never started, it returns null
.
sinceStart(): number | null
Example:
let timer = new Timer(10000);
timer.start();
timer.stop();
timer.timeRemaining();
timer.start();
timer.timeRemaining();
timer.sinceStart();
reset()
This method resets the timer back to full delay, and keeps its current state.
If the stopwatch was never started, it does nothing.
reset(): boolean
Returns false
if the stopwatch was never started, otherwise true
.
Example:
let timer = new Timer(10000);
timer.start();
timer.timeRemaining();
timer.reset();
timer.timeRemaining();
timer.isActive();
toString()
This method returns the time remaining out of the full delay, with ms
string added at the end.
toString(): string
Example:
let timer = new Timer(10000);
timer.start();
timer.toString();
Properties
Timer, unline Stopwatch, only has two properties:
onfinish
This property should be a function, and can be freely changed.
It gets called if the timer onstart
reaches 0 milliseconds.
delay
This property is read-only.
It stores the maximum delay in ms after which the callback function should be called.
Constants
Clock Functions library also provides a few useful time-related constants:
SECOND
- equal to 1,000 milliseconds, or 1 second.MINUTE
- equal to 60,000 milliseconds, or 1 minute.HOUR
- equal to 3,600,1000 milliseconds, or 1 hour.DAY
- equal to 86,400,000 milliseconds, or 24 hours.MONTH
- equal to 2,592,000,000 milliseconds, or 30 days.YEAR
- equal to 31,536,000,000 milliseconds, or 365 days.