Stopwatch in JavaScript
Create a stopwatch
BeginnerMini ProjectsExample 3 of 20
stopwatch.js
1class Stopwatch {2 constructor() { this.startTime = null; this.elapsed = 0; }3 start() { this.startTime = Date.now() - this.elapsed; }4 stop() { if(this.startTime) this.elapsed = Date.now() - this.startTime; }5 reset() { this.elapsed = 0; this.startTime = null; }6 getTime() { return this.elapsed; }7}
Output
// Stopwatch functionality
What's going on
Stopwatch measures elapsed time.