• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / How to Create a JavaScript Count Up Timer

How to Create a JavaScript Count Up Timer

April 9, 2022 Leave a Comment

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

setTimeout(function(){
  //Code that will run after 5 seconds
}, 5000);
setInterval(function(){
  //Code that will run every 1 second
}, 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 5 seconds. We can do this easily using the setTimeout method.

Here is the JavaScript code that can make this happen.

setTimeout(function(){
  alert("5 seconds have passed.")
}, 5000);

Let’s take a look at another example below.

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

In this example, we will use the setTimeout() method to create a simple count up timer that will display a message when finished. It will start when the user presses a button. It will count up to 5 seconds and then display a message.

Here is the simple HTML setup:

<div id="div1">
  <p>Click the button below to start the count up timer</p>
  <div id="click-me" onclick="startCountUp()">Start Timer</div>
  <div id="countup-text"></div>
</div>

To make this count up timer happen, we will use the setTimeout() method and the setInterval() method. We will set the setTimeout method to run after 5 seconds.

While the setTimeout() method is waiting to execute its function call, we will provide the user with a count up timer 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 will count up to 5 seconds. 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 5.

Finally, in our setTimeout() method we will have an alert box alert the user when the count up timer is done. We will also add some code to make it so the user can’t run the count up 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 startCountUp(){

  if( isSetTimmeoutRunning == false ){
    //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;
    
    //Alert the user when 5 seconds have passed
    setTimeout(function(){
      isSetTimmeoutRunning = false;
      //Send alert message to the user
      alert(5 + " seconds have passed.");
    }, 5000);
    
    //The initial starting point of the counter is 0
    var counter = 0;
    
    document.getElementById("countup-text").innerHTML = "<b>" + 0 + "</b>";

    var interval = setInterval(function(){
      counter++;
      document.getElementById("countup-text").innerHTML = "<b>" + counter + "</b>";
      if( counter == 6 ){
        document.getElementById("countup-text").innerHTML = "";
        clearInterval(interval);
      }
    }, 1000);
  }
}

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

Code Output:

Click the button below to start the count up timer

Start Timer

Full Code:

<div id="div1">
  <p>Click the button below to start the count up timer</p>
  <div id="click-me" onclick="startCountUp()">Start Timer</div>
  <div id="countup-text"></div>
</div>

<script>

  var isSetTimmeoutRunning = false;
  function startCountUp(){

  if( isSetTimmeoutRunning == false ){
    isSetTimmeoutRunning = true;
    
    setTimeout(function(){
      isSetTimmeoutRunning = false;
      alert(5 + " seconds have passed.");
    }, 5000);
    
    var counter = 0;
    
    document.getElementById("countup-text").innerHTML = "<b>" + 0 + "</b>";

    var interval = setInterval(function(){
      counter++;
      document.getElementById("countup-text").innerHTML = "<b>" + counter + "</b>";
      if( counter == 6 ){
        document.getElementById("countup-text").innerHTML = "";
        clearInterval(interval);
      }
    }, 1000);
  }
}

</script>

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

Other Articles You'll Also Like:

  • 1.  Using JavaScript to get the Last Element in Array
  • 2.  JavaScript Check If Number is a Whole Number
  • 3.  Using JavaScript to Remove the First Character From a String
  • 4.  Remove Braces from String Using JavaScript
  • 5.  Get the Sum of Array in JavaScript
  • 6.  Using JavaScript to Convert Month Number to Name
  • 7.  Using JavaScript to Round to 4 Decimal Places
  • 8.  Using JavaScript to Resize an Image
  • 9.  Using JavaScript to Capitalize the First Letter of a String
  • 10.  JavaScript offsetTop – Get the Top Position of Element

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy