How to use Countdown timer in javascript

  1. Cross browser compatibility.
  2. Doesn’t get unnecessary calls.
  3. Easy to implement.
  4. Work smoothly and perfectly.

Just you have to do is to set a specific target date in the future, get the current date and then calculate the remaining time between them.

So let’s have a look over the example given below

Countdown timer in javascript

Countdown.html

And here's the html code of this example that you will copy and paste in your web page

z-index: 1000; bottom: 0px; left: 0px; font-size: 20px;font-family:verdana;">Days

z-index: 1000; bottom: 0px; left: 0px; font-size: 20px;font-family:verdana;">Hrs

z-index: 1000; bottom: 0px; left: 0px; font-size: 20px;font-family:verdana;">Mins

z-index: 1000; bottom: 0px; left: 0px; font-size: 20px;font-family:verdana;">Secs

z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;"

id="days1">

position: absolute; z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;

">

position: absolute; z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;

">

z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;"

id="hrs2">

position: absolute; z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;

">

position: absolute; z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;

">

position: absolute; z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;

">

position: absolute; z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;

">

This countdown timer function will give you countdown between current date, that it will pick from your system date, and the date mentioned in calling of countdown timer function which is countdown(2011, 12, 25);

You just have to replace this date with the date you desire for countdown time. I have used jquery’s $(document).ready(function ()) event because I want to execute this function when every DOM is loaded. But if you don’t want to use jquery and its ready event then exclude the jquery file and its code from the example and copy/paste following code just before tag

This technique is equivalent to jquery’s document.ready event.

I have used two divs for showing days, two divs for showing hours, two divs for showing minutes and again two divs for showing seconds. For example if remaining days are 14 then 1 will be store in days1 div and 4 will be stored in days2 div. You can use one div for showing remaining days, hours, minutes and seconds.

Moreover, I have applied all the checks in javascript code so that function will not get any extra call if countdown is finished, I have seen lot of code in internet that work perfectly but when countdown is finished but its code is still in web page then the countdown function gets extra and totally unnecessary calls but in my example I have taken care of this issue and avoid extra calls to the countdown timer function.

0 comments:

Post a Comment