SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Game timer

  1. #1
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    How is your counter keeping track of the time? You need to use separate counters to keep track of multiple times.

    One way of doing that is by telling the countdown function which counter you want it to control.
    Do you want to give that a go?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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