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:
Click the button below to start the count up timer
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 = "" + 0 + "";
var interval = setInterval(function(){
counter++;
document.getElementById("countup-text").innerHTML = "" + counter + "";
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
Full Code:
Click the button below to start the count up timer
Hopefully this article has been useful for you to understand how to create a JavaScript count up timer.
Learn more about it here.