javascript - setInterval De incrementing countdown does not stop at Zero -


i busy making red or blue clicking game can viewed here: http://www.ysk.co.za/red-blue problem countdown have made de incrementing var countdown stops when function key() run, , until run countdown goes negative values if more 12 seconds
javascript is:

var = 0; var seconds = 0; var countdown = 12; setinterval(function () {     $("#counter").html(countdown.tofixed(2)); }, 10);  function play() {     $("#num").select(); }  function key(e) {     var code = e.keycode ? e.keycode : e.charcode;     var random = math.random();     if (random <= 0.5) {         $("#redblue").css("background-color", "red")     } else {         $("#redblue").css("background-color", "blue")     }     if (code === 86) {         $("#red").css("background", "#ff4e00");         settimeout(function () {             $("#red").css("background-color", "red")         }, 500);         var color = "rgb(255, 0, 0)";     } else if (code === 66) {         $("#blue").css("background-color", "#006bff");         settimeout(function () {             $("#blue").css("background-color", "blue")         }, 500);         color = "rgb(0, 0, 255)";     }     var score = / seconds;     var endscore = (i - 1) / seconds;     if ($("#redblue").css("background-color") != color) {         alert("you clicked " + + " times in " + seconds.tofixed(2) + " seconds. score                          " + score.tofixed(2));         document.location.reload(true);     } else if ($("#redblue").css("background-color") == color) {         $("#score").html("");         i++;     }     if (i == 1) {         setinterval(function () {             seconds += 0.01         }, 10);         setinterval(function () {             countdown -= 0.01         }, 10);     };     if (countdown < 0) {         alert("you clicked " + (parseint(i) - 1) + " times in 12 seconds. score           " + endscore.tofixed(2));         document.location.reload(true);     } } 

my html is:

<div id="game"> <div id="redblue"></div> <div id="red" style="background:red;"></div> <div id="blue" style="background:blue;"></div><div class="clear"></div> <button onclick="play()">start</button><br>enter "v" if block red or "b" if block blue <input type="text" size="2" maxlength="1" id="num" onkeyup="key(event); this.select()" />  </div> <div id="counter" style="font-family:monospace;font-size:100px;font-    weight:600;float:left;margin-left:40px;"></div> <div id="score"></div>` 

how can when countdown == 1 alert() appears, if know of better way stop , reset game other refreshing page great.
appreciated

since updating time in setinterval, should check in there whether countdown value has reached 0

setinterval(function () {         countdown -= 0.01;         if (countdown < 0) {             alert("you clicked " + (parseint(i) - 1) + " times in 12 seconds. score           " + endscore.tofixed(2));             document.location.reload(true);         }     }, 10); 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -