Jump to content

Game timer


kadi

Recommended Posts

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>
Link to comment
Share on other sites

I'm not sure I follow what you're trying to accomplish. Could you please explain more what you're trying to do? Multiple counters of some sort?

Link to comment
Share on other sites

I couldn't get your code to run. This version is probably better...

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>timer</title><script>var CCOUNT = 80;var count = CCOUNT;var t = null;var timespan;window.onload = init;function init(){ timespan = document.getElementById('timespan'); document.getElementById('btnStart').onclick = start; document.getElementById('btnPause').onclick = cdpause; document.getElementById('btnReset').onclick = cdreset; cddisplay();}function cddisplay() {  timespan.innerHTML = "Time Left: " + count;}   function start(){ // starts countdown  if (t==null && count>0){    timespan.style.color = 'red';    countdown();  }}function countdown() {  cddisplay();  if (count == 0) {    timespan.style.color = 'black';    alert('time is up');    location.reload(); // reload page  }else{     count--;     t = setTimeout("countdown()", 100);  }}   function cdpause() { // pauses countdown  clearTimeout(t);  t = null;  timespan.style.color = 'black';}   function cdreset() { // resets countdown  cdpause();  count = CCOUNT;  cddisplay();  t = null;  timespan.style.color = 'black';}   </script></head> <body><span id="timespan"></span><hr/><input type="button" value="Start" id="btnStart"/><input type="button" value="Pause"  id="btnPause"/><input type="button" value="Reset" id="btnReset"/></body></html>
Edited by davej
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...