Class: Clock¶
Simple stopwatch for measuring elapsed time.
Records a start and stop timestamp and returns the difference in milliseconds.
Calling elapsed() before stop() returns the time since start() was called.
Example¶
const timer = new Clock();
timer.start();
// ... your code ...
timer.stop();
console.log(`Elapsed: ${timer.elapsed()}ms`);
Since¶
1.0.0
Constructors¶
Constructor¶
new Clock():
Clock
Create a new stopwatch. Both timestamps are null until started.
Returns¶
Clock
New Clock instance
Since¶
1.0.0
Gears¶
start()¶
start():
void
Record the start timestamp and clear any previous stop time.
Returns¶
void
void
Example¶
const timer = new Clock();
timer.start();
console.log(timer.isRunning()); // true
Since¶
1.0.0
stop()¶
stop():
void
Record the stop timestamp.
Returns¶
void
void
Example¶
const timer = new Clock();
timer.start();
timer.stop();
console.log(timer.isRunning()); // false
Since¶
1.0.0
reset()¶
reset():
void
Clear both timestamps and return the timer to its initial state.
Returns¶
void
void
Example¶
const timer = new Clock();
timer.start();
timer.stop();
timer.reset();
console.log(timer.isRunning()); // false
Since¶
1.0.0
elapsed()¶
elapsed():
number
Return elapsed time in milliseconds.
If the timer has not been stopped yet, returns the time since start() was called.
Returns¶
number
Elapsed time in milliseconds
Throws¶
Error if start() has not been called
Example¶
const timer = new Clock();
timer.start();
timer.stop();
console.log(`Time: ${timer.elapsed()}ms`);
Since¶
1.0.0
isRunning()¶
isRunning():
boolean
Return true if the timer has been started but not yet stopped.
Returns¶
boolean
True if started and not stopped
Example¶
const timer = new Clock();
timer.start();
console.log(timer.isRunning()); // true
timer.stop();
console.log(timer.isRunning()); // false
Since¶
1.0.0
getStartedTime()¶
getStartedTime():
number|null
Return the Unix timestamp (ms) when start() was called, or null if not started.
Returns¶
number | null
Start timestamp in milliseconds, or null
Since¶
1.0.0
getStoppedTime()¶
getStoppedTime():
number|null
Return the Unix timestamp (ms) when stop() was called, or null if not stopped.
Returns¶
number | null
Stop timestamp in milliseconds, or null
Since¶
1.0.0