Countdown Timer

Build a countdown timer

IntermediateTopic: Mini Projects
Back

JavaScript Countdown Timer Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
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.

Table of Contents