Click here to Skip to main content
15,849,996 members
Articles / Programming Languages / Javascript

JavaScript: Easy Creation of "Countdown Timers"

Rate me:
Please Sign up or sign in to vote.
1.70/5 (3 votes)
10 May 2015CPOL2 min read 5.3K   4  
JavaScript: Easy Creation of "Countdown Timers"

Image 1

If there is one thing I don't like doing, it's doing the same thing twice or even more! So I was building an application on the side where I need to have a listing that performs a simple countdown. At this point, I've been lazy and just have it counting down the seconds, but this example would be really easy to update to put a proper countdown of days, hours, minutes, and seconds and heck if you get really adventurous even weeks, months, and years!

In this example, I have a listing of items. Each item has a separate countdown timer associated with it that can be different.  This will be some simple HTML code:

<div id="items">
<h1>Item Title 1</h1>
<h2>Available in: <span id="item_1"></span><span id="item_done_1">Ready!</span></h2>
<h1>Item Title 2</h1>
<h2>Available in: <span id="item_2"></span><span id="item_done_2">Ready!</span></h2>
<h1>Item Title 3</h1>
<h2>Available in: <span id="item_3"></span><span id="item_done_3">Ready!</span></h2>

Now that we have three items with a spot each to indicate when it is available, JavaScript code is required to create, display, and update the counter. The first part of the code below will create the counter objects.

var currentTime = new Date();
currentTime = currentTime.getTime();  // get the current time in milliseconds

var counter_1 = new Object();
counter_1.time = currentTime;
counter_1.reward_time = 10;
counter_1.finished = '#item_done_1'; = '#item_1';


var counter_2 = new Object();
counter_2.time = currentTime;
counter_2.reward_time = 600;
counter_2.finished = '#item_done_2'; = '#item_2';


var counter_3 = new Object();
counter_3.time = currentTime;
counter_3.reward_time = 1200;
counter_3.finished = '#item_done_3'; = '#item_3';


Now that the counters are created, several more functions are required. The first is the mystery AddCounter function that is used several times above:

var counters = new Array();
function AddCounter(counter) {
counter_found = false;
for (var x = 0; x < counters.length; x++) {
if (counters[x].id == {
counter_found = true;

if (!counter_found) {
counters[counters.length] = counter;

The above code might be a bit of an overkill as I'm checking to make sure that the same counter is not created more than once in the array.

The final piece of code is creating a new interval that will fire every 1 second (1,000 milliseconds) and update all of the counters:

setInterval(UpdateCounters, 1000);

function UpdateCounters() {

for (var x = 0; x < counters.length; x++) {
var counter = counters[x];
var time_left = counter.reward_time - (currentTime - counter.time);
var message = time_left + ' second';

if (time_left != 1) {
message += 's';

if (time_left < 0) {
} else {

In the final example, I loop through the counters array and perform a calculation to determine the number of seconds remaining and then update the message to display it.

Once the counter reaches 0, the message is removed and a hidden element is shown indicating that the counter is done. This is great to display a link or form to allow the user to perform some sort of action.

Please note that Jquery is used for simplification of accessing the DOM.


Using a basic interval and looping through an array with a set of standard sub objects, JavaScript can easily manipulate the DOM to update any number of counters.

View the full source on my Git repo:

Photo courtesy of jovike


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Comments and Discussions

-- There are no messages in this forum --