Stopwatch

Create a stopwatch

JavaScriptBeginner
JavaScript
class Stopwatch {
    constructor() { this.startTime = null; this.elapsed = 0; }
    start() { this.startTime = Date.now() - this.elapsed; }
    stop() { if(this.startTime) this.elapsed = Date.now() - this.startTime; }
    reset() { this.elapsed = 0; this.startTime = null; }
    getTime() { return this.elapsed; }
}

Output

// Stopwatch functionality

Stopwatch measures elapsed time.