Countdown Timer
Build a countdown timer
IntermediateTopic: Mini Projects
JavaScript Countdown Timer Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
class Countdown {
constructor(target) { this.target = target; }
start(callback) {
const interval = setInterval(() => {
const remaining = this.target - Date.now();
if(remaining <= 0) { clearInterval(interval); callback(); }
else callback(remaining);
}, 1000);
}
}Output
// Countdown timer
Understanding Countdown Timer
Countdown Timer counts down to target 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.