www.webdeveloper.com
Results 1 to 2 of 2

Thread: Game timer

  1. #1
    Join Date
    Jul 2013
    Posts
    12

    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');
               phoneui.gotoPage('m1-HOMESCREEN','NONE');
               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
    Join Date
    Jun 2008
    Posts
    106
    Your code doesn't even work until you hit Reset because you never give "count" a starting value.

    Besides that, your code works for one level.
    Here's an example for 3 levels: http://pastebin.com/KwCjME5U

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles