Game timer

Discussion in 'JavaScript' started by kadi, Aug 2, 2013.

  1. #1
    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.

    <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>
    Code (markup):

     
    kadi, Aug 2, 2013 IP
  2. juggalox

    juggalox Member

    Messages:
    152
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    33
    #2
    Create a variable which holds count-down time with global scope in your JS , and change value of that depending on your level , and call functions as usual which use this variable to countdown from.
    In your code , I see nothing about levels and their countdown values. Your code just reloads the page and ofcourse the whole process repeats. You must either not-reload and use the global variable method I suggested above or reload with a change in the script. The change could be a javascript code change to load a new level page which has a higher countdown value , or change the same page's JS dynamically with PHP like index.php?level=2 gives new JS with higher countdown.

    Also , take a look at : http://papermashup.com/read-url-get-variables-withjavascript/
    You might be able to do this with JS alone using the above hint.
     
    juggalox, Aug 9, 2013 IP