In JavaScript, we can make a countdown timer somewhat easily with the help of the setTimeout and setInterval methods.

setTimeout(function(){
  //Code that will run after 5000 milliseconds
}, 5000);
setInterval(function(){
  //Code that will run every 1000 milliseconds
}, 1000);

You can see in the code above that the setTimeout() method will run the function after 5000 milliseconds. We can change that parameter to be however long we want to wait to execute the function call.

On the other hand, the setInterval() method will run the function every 1000 milliseconds. We can change that parameter to be however often we want to execute the function call.


Let’s say we want to run code that executes an alert box after 10 seconds. We can do this easily using the setTimeout method.

Here is the JavaScript code that can make this happen.

setTimeout(function(){
  alert("10 seconds have passed.")
}, 10000);

Let’s take a look at another example below.

Creating a JavaScript Countdown Timer Using the setTimeout() and setInterval() Methods

In this example, we will use the setTimeout() method to create a simple countdown timer that will display a message when finished. It will start when the user presses a button. We will also let the user enter the number of seconds they want the countdown timer to run for.

Here is the simple HTML setup:

How many seconds would you like the countdown timer to start from?

Start Countdown

To make this countdown timer happen, we will use the setTimeout() method and the setInterval() method. We will first get the number of seconds the user has entered into the input field using the value property. We will use this as our start number for the timer.

Once we have the seconds the user has given us, we will multiply that number by 1000 to convert it to milliseconds. We will then use that as our milliseconds parameter for the setTimeout() method.

While the setTimeout() method is waiting to execute its function call, we will provide the user with a countdown by using the setInterval() method. The setInterval() method will run every second until it is told to stop.

Inside the setInterval() method, we will run a function that runs every second. The function will display how many seconds remain in the countdown timer. We will use the innerHTML property to update the text for the user.

We will end the setInverval() method using the clearInterval() method when our counter has reached 0.

Finally, in our setTimeout() method we will have an alert box alert the user when the countdown timer is done. We will also add some code to make it so the user can’t run the countdown timer again until it has completed its cycle.

Here is the JavaScript code:

//The variable below will just make it so the user cannot run the setTimeout method more than once at a time
var isSetTimmeoutRunning = false;
  
function startCountdown(){

  if( isSetTimmeoutRunning == false ){
    //Get the seconds the user has given us and convert number to an Integer
    var userInput = parseInt(document.getElementById("userInput").value);

    //Make sure the user has entered a number greater than 0
    if( userInput >=1 ){
      //We set this variable to true when we first run the setTimeout method.
      //It will get set back to false when the setTimeout method has completed
      isSetTimmeoutRunning = true;

      //Next, we will convert the seconds to milliseconds
      var userMilliseconds = userInput*1000;
    
      setTimeout(function(){
        isSetTimmeoutRunning = false;
        //Send alert message to the user
        alert(userInput + " seconds have passed.");
      }, userMilliseconds);
    
      //The initial starting point of the counter is the user inputted seconds
      var counter = userInput;
    
      document.getElementById("countdown-text").innerHTML = "" + counter + "";

      var interval = setInterval(function(){
        counter--;
        document.getElementById("countdown-text").innerHTML = "" + counter + "";
        if( counter == 0 ){
          document.getElementById("countdown-text").innerHTML = "";
          clearInterval(interval);
        }
      }, 1000);
    }
  }
}

The final code and output for this example of how to create a JavaScript countdown timer is below:

Code Output:

How many seconds would you like the countdown timer to start from?

Start Countdown

Full Code:

How many seconds would you like the countdown timer to start from?

Start Countdown



Hopefully this article has been useful for you to understand how to create a JavaScript countdown timer.

Categorized in:

JavaScript,

Last Update: May 3, 2024