Stopwatch
Create a stopwatch
BeginnerTopic: Mini Projects
JavaScript Stopwatch Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
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
Understanding Stopwatch
Stopwatch measures elapsed time.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.