Products that might interest you: Joomla Subscription Component, Drupal Subscription Module, PHP Nuke Subscription

jQuery Count Down

There are numerous times when you need a count down timer. This can be done without jQuery but with many websites using jQuery, why not write this tutorial for it :)

The example below is the jQuery count down in action.

Go Daddy $1.99 Web Hosting

Now you see it in action, let's get to the codes. It is really simple actually. I will assume that you have already included the jquery javascript in the header of your page. If you have not already done that, please read up on how to include jquery core file.

First we open javascript tag

  1. <script type="text/javascript">

Our next step is to write a var that will contain the total lenght of the timer. This is in seconds and in our example it is 50

  1. var wsCount = 50;//this is the count down time

We next need to use jQuery to start our count down on document load. Our count down is inside a function that I will explain later.

  1.  
  2. $(document).ready(function() {
  3. startCountdown();//load the count down function on document loaded.
  4. });
  5.  

As you can see we are running the function startCountdown() when the document has finished loading.
Now we write the count down function that will reload itself until the value is less than 0

  1.  
  2. function startCountdown() {
  3. if((wsCount - 1) >= 0){
  4. wsCount = wsCount - 1;
  5. $("#ws_counter").html('Counting down <font style="font-size:125%;"> <b>' + wsCount + '</b></font> seconds.');
  6.  
  7. setTimeout(startCountdown, 1000);
  8. }
  9. else{
  10. $("#ws_counter").html('You have reached the end of the count down');
  11. }
  12.  
  13. }
  14.  

The startCountdown function is really simple, what is does is first check to see if the var wsCount is 0, if not it will load content in anything that has the id ws_counter. The function will then minus 1 from the count until it reaches 0.
It will also reload the function every second(setTimeout(startCountdown, 1000); until the var wsCount value is 0. Once it is 0, the function will then update the id ws_counter with the text 'You have reached....'
In our example, the id ws_counter is a paragraph with styling.

  1. <p id="ws_counter"></p>

Let's put the entire codes together.

  1.  
  2. <script type="text/javascript">
  3.  
  4.  
  5. var wsCount = 50;
  6.  
  7. $(document).ready(function() {
  8. startCountdown();
  9. });
  10.  
  11. function startCountdown() {
  12. if((wsCount - 1) >= 0){
  13. wsCount = wsCount - 1;
  14. $("#ws_counter").html('Counting down <font style="font-size:125%;"> <b>' + wsCount + '</b></font> seconds.');
  15.  
  16. setTimeout(startCountdown, 1000);
  17. }
  18. else{
  19. $("#ws_counter").html('You have reached the end of the count down');
  20. }
  21.  
  22. }
  23.  
  24.  
  25.  
  26. </script>
  27. <p id="ws_counter" class="message"></p>
  28.  
  29.  

Very simple uh? You can use this in your scripts to even countdown until christmas or birthday and so on. We are available for custom work. Post a project to our workboard.

Leave a comment

 
 

Purchase Subscription

Featured Downloads

Added on 07/17/2009
1210 views
Subscription
Added on 12/20/2009
1552 views
Subscription
Added on 05/30/2011
3996 views
Subscription

Recently Downloaded

WS Subscription v4 PHP Nuke
Downloaded: 2 days ago
WS Groups Joomla 1.7+
Downloaded: 13 days ago
Joomla Reset Admin Password Tool
Downloaded: 14 days ago
WS Groups Joomla Standalone
Downloaded: 15 days ago
Joomla Reset Admin Password Tool
Downloaded: 18 days ago
Joomla Reset Admin Password Tool
Downloaded: 19 days ago
Satisfaction Guarantee Badge Seal
Downloaded: 24 days ago

Latest Jobs

WS Downloads
Listed:
Auto post Joomla article to twitter
Listed:
Facebook Events for Joomla
Listed:
WS Groups Joomla Integration
Listed:
Joomla Google Calendar
Listed: