Build a count down in Node.js

Hello programmers, In this article I will show you how we can build a simple countdown app in Nodejs.
Before we get started with the building process, we need to know a few concepts. Let’s first discuss them one by one.

1. setInterval()

The setInterval function helps us execute a block of code repeatedly after a given interval of time. The code runs repeatedly until it is stopped.

Syntax –

setInterval ( expression, interval );

2. clearInterval()

The clearInterval function helps to stop further calls by the setInterval function.

Syntax –

// This line of code will run expression repeatedly after given interval of time.
let id = setInterval ( expression, interval );

// This line of code will stop the repeatedly calling of expression after 6000 ms.
setTimeout(() => { clearInterval(id);}, 6000);

 

As we are clear with our basic concepts of setInterval() and clearInterval() we can proceed further to build our app using these functions.

First, we will define our timer starting time

//Here we are starting with 15 seconds.
let time = 15;

 

As we need to update the current remaining time each second, it is obvious we will be using setInterval() function to update the current remaining time and print it.

// Here we are starting with 15 seconds.
let time = 15;

// Here our update function will run every 1000 ms or 1 sec.
// We will be writing our logic of updating time and printing it in update function.
let countdown = setInterval(update, 1000);

Now it’s time to define our update function. I will first write the code and explain every step inside the same with comments.

let time = 15; 
let countdown = setInterval(update, 1000);

function update() {
    // Here were are calculating minutes and second from given time.
    let min = Math.floor(time / 60);
    let sec = time % 60;

    //Here we are printing our current time.
    console.log(`${min}:${sec}`);

    // Here we are reducing our time by 1. As this function is called every 1 sec so our time will reduce by 1 every 1 second.
    time--;

    // Stop when min and sec are 0.
    if (min == 0 && sec == 0) clearInterval(countdown);
}

As of now, our code is complete and our output will look like this if we give time as 15.

0:15
0:14
0:13
0:12
0:11
0:10
0:9
0:8
0:7
0:6
0:5
0:4
0:3
0:2
0:1
0:0

As you can see 10 is printed as 0:10 but 9 is printed as 0:9 instead of 0:09. We can format this as well.

// formatting our sec to be two digit if less than 10. 
sec = sec < 10 ? "0" + sec : sec;

Our final code looks like this –

let time = 15;
let countdown = setInterval(update, 1000);

function update() { 

let min = Math.floor(time / 60); 
let sec = time % 60; 

sec = sec < 10 ? "0" + sec : sec;

console.log(`${min}:${sec}`); 

time--;
if (min == 0 && sec == 0) clearInterval(countdown);

}

Output –

0:15
0:14
0:13
0:12
0:11
0:10
0:09
0:08
0:07
0:06
0:05
0:04
0:03
0:02
0:01
0:00

You may also learn,

Leave a Reply

Your email address will not be published. Required fields are marked *