Thread: Game timer

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    11
    Rep Power
    0

    Game timer


    how a game timer is designed for many levels with different times i.e., for Level1 10 sec, Level 2 20 sec, etc. i got a code but its not working for Levels individually as its overlapping and not working.
    Code:
    <script>
        
        var CCOUNT = 8;
        
        var t, count;
        
        function cddisplay() {
            // displays time in span
            document.getElementById('timespan').innerHTML = "Time Left:" + count;
        };
        
        function countdown() {
            // starts countdown
            cddisplay();
            if (count == 0) {
              alert('time is up');
                         location.reload();                      
            } else {
                count--;
                t = setTimeout("countdown()", 1000);
            }
        };
        
        function cdpause() {
            // pauses countdown
            clearTimeout(t);
        };
        
        function cdreset() {
            // resets countdown
            cdpause();
            count = CCOUNT;
            cddisplay();
        };
        
    </script>
    
    <body onload="cdreset()">
    <span id="timespan"></span>
    <input type="button" value="Start" onclick="countdown()">
    <input type="button" value="Stop" onclick="cdpause()">
    <input type="button" value="Reset" onclick="cdreset()">
    </body>
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,165
    Rep Power
    184
    You can use conditional validation (ie: if...else conditions, switch statements, while loops, ternary operators, etc), based on each specific level, to set a certain length of time with the setTimeout().

IMN logo majestic logo threadwatch logo seochat tools logo