October 19, 2015 10:31 pm

Creating a countdown timer with Vanilla JavaScript

Adding a timer with JavaScript to a website can be useful for various purposes. The countdown can be counting the days left to the official launch of your website, product or feature within the website. Relying on third-party libraries can cause extra burden on the bandwidth involve and it can    lose some of your time trying to bend the library to your will. If all you need is a simple countdown timer, the code involved is not that much to need the involvement of third-party scripts.
Creating a countdown timer with Vanilla JavaScript

To use our countdown timer, all that you are going to need to do is call the addCountdown function, pass it a date string and an element where the countdown will go and you are set. Using it looks something like this:

You need one line of JavaScript and one line that adds the element where you want it to be in the DOM:

The result is a countdown timer like this one:

Creating a countdown timer

The visuals

We have created a simple dark skin for the countdown timer.

We float each box (there are 4 boxes – with days, hour, minutes and seconds) to the left, give fixed dimensions to the countdown timer and its boxes, make it dark and highlight the boxes with white border. We also use the default cursive font available to the system.

The logic

We create our function which accepts a date to countdown to, the id of the element which we want to hold the countdown timer and an optional array with labels for the time values. It seeks for 4 elements in the following order – days, hours, minutes, seconds. For example, if we pass it an array like: [“Дни”, “Часа”, “Минути”, “Секунди”] the labels will be in Bulgarian as shown in the picture below:

Creating a countdown timer1

To achieve this, we just called the addCountdown function in the following manner:

In the body of the addCountdown function we define another function called timeRemaining which will determine how many days, hours, minutes and seconds remain to the target date and return them as a string combined with the appropriate time label.

Afterwards, we define yet another function which takes the return value from the timeRemaining function and if timeRemaining has returned null we stop updating the clock every second and remove the countdown container (it returns null if the UNIX timestamp holds a negative value as checked in  if (timeRemaining > 0) )

Then, we call timeRemaining and save the return value to a variable:

Now, in our main addCountdown function we call isCountdownEnded and return from it if the result of the call to isCountdownEnded contains a truthy value:

Progressing further, we add all necessary children to the container element for the countdown timer with their appropriate value:

times is an array that comes as a result of the call to timeRemaining.

Finally, we set an interval that will call timeRemaining, stop further execution if the timer’s time is up and which will update the remaining time each second.

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter


Tutorial Categories:
  • Syed Faizan Ul Haq

    Thank you Ivan 🙂

  • Ivan Yosifov

    Why is it not animated?

    • Ivan Dimov

      Hi Ivan,
      feel free to edit it in any way you want.

  • Green Life IT

    this is very cool…..

  • Green Life IT

    this is very cool…..