SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Countdown Timer
By Scott Isaacs

Create a countdown timer. Use this timer when you want an action to occur after a certain amount of time.

To experiment with this example, type in a number in the text box below and click on "Go". The timer will count down the seconds.

Count down seconds.

Action will occur in n seconds.

Description

The element with the ID of "howMany" contents are updated with the countdown. To start the countdown, the function countDown() needs to be called. The countDown() function looks at the value contained within the element, and automatically decrements from that value until 0.

Code

  function countDown() {
    var el = document.all.howMany   
    el.innerText--  
    if (0<=el.innerText)
      setTimeout("countDown()",1000)
    else {
      // Replace following with custom action.
      el.innerHTML = "Do Action Now"
      // The following line is only necessary to reenable the "Go" button 
      document.all.go.disabled=false;
    }
  }
Discuss and Rate this Article