Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript timer countdown

    Trying to write a javascript countdown script. Here's what I got:

    Code:
    function timer(position,count)
     {
      var newCount
      if (position == 'start') {
       document.getElementById("timer").innerHTML=count
       newCount = count - 1
       alert(newCount);
       setTimeout("timer('go',newCount)",1000)
      }
      
      if (position == 'go') {
       document.getElementById("timer").innerHTML=count
      }
     }
    </script>
    </head>
    
    <body onload="timer('start',10);">
    onload starts the timer at 10 and then my first step was to have to go down to 9. But I get this javascript error message:

    Error: newCount is not defined

    But I have it as an alert right before I call it and it alerts the variable just fine. Why would it say it isnt defined?

    Live script: http://webwork.shasta.com/mmo/battle.asp

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You were close. First newCount needs to be a global variable the way it is set up. Second, the 'go' state needs to decrement newCount and reset the timer.

    Code:
    <html>
    <head>
    <title>Test</title>
    <script type="text/javascript"> 
    var newCount;
    function timer(position,count)
     {
      if (position == 'start') {
       document.getElementById("timer").innerHTML=count
       newCount = count - 1
       setTimeout("timer('go',newCount)",1000)
      }
      
      if (position == 'go') {
       document.getElementById("timer").innerHTML=count
       newCount--;
       setTimeout("timer('go',newCount)",1000)
      }
     }
    </script>
    </head>
    <body onload="timer('start',10);">
      <div id="timer" />
    </body>
    </html>
    david_kw

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can u teach me how to do it? I don't understand the setTimeout portions

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Presumably the countdown should stop at 0 and not continue negative.

    so:-

    if (position == 'go') {
    document.getElementById("timer").innerHTML=count;
    newCount--;
    if (newCount >= 0) {
    setTimeout("timer('go',newCount)",1000);
    }
    }

    gatw - The setTimeout() method is used to cause an action to repeat. Two arguments are required, the name of the function to call and the required delay in millseconds.

    Example:

    var count = 0;
    function neverends() {
    count ++;
    alert ("This is ten-second message No: " + count );
    window setTimeout ("neverends()", 10000);
    }

    neverends(); // call the function after it has been declared to start the timer.

    Note that subsequent staements are not delayed - the delay only applies to the specific function called.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •