Countdown script on site load?

Discussion in 'JavaScript' started by Rezo, May 2, 2009.

  1. #1
    Dear friends,

    I need your help :) I need a script that starts the countdown on site load.

    For example, user loads the page and I need a countdown from 5 minutes to 0 minutes and 1 second, and I need it do be displayed on the site interactively.

    I hope you understood. Thanks for your help in advance.

    Regards,
    Radu
     
    Rezo, May 2, 2009 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    
    <div id="divTimer"></div>
    <script type="text/javascript">
    var t = 20;
    var divT = document.getElementById("divTimer");
    window.onload = countdown;
    function countdown() {
        if(t > 0) {
            divT.innerHTML = parseInt(t/60) + " minutes, " + (t % 60) + " seconds";
            t = t - 1
            setTimeout("countdown()", 1000);
        }
    }
    </script>
    
    Code (markup):
     
    camjohnson95, May 4, 2009 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    or to make it more 'clock-like':
    
    <div id="divTimer"></div>
    <script type="text/javascript">
    var t = 300;
    var divT = document.getElementById("divTimer");
    var d = new Date();
    
    window.onload = countdown;
    function countdown() {
        if(t > 0) {
            d.setMinutes(parseInt(t/60));
            d.setSeconds(t%60);
            divT.innerHTML = d.toTimeString().substr(3,5);
            t = t - 1
            setTimeout("countdown()", 1000);
        }
    }
    </script>
    
    Code (markup):
     
    camjohnson95, May 4, 2009 IP
  4. Rezo

    Rezo Well-Known Member

    Messages:
    4,183
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Hey,

    thanks alot.

    Is this the full including the code to display it?

    Regards,
    Radu
     
    Rezo, May 4, 2009 IP
  5. JavaScriptBank.com

    JavaScriptBank.com Peon

    Messages:
    141
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    JavaScriptBank.com, May 4, 2009 IP
  6. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    yes, the timer will be displayed wherever the line:
    <div id="divTimer"></div>
    is placed... just make sure that the script is placed after the div.
     
    camjohnson95, May 5, 2009 IP