showing number of clicks within 10 seconds using jquery -


i'm trying develop game requires user rapidly click button seconds set 10, timer starts when click button, there's show me button shows result, , after 10 seconds total should popup.

my problem can't seem increments right, , clicks still register after timer done shouldn't do. need help.

$(document).ready(function() {     $('#total').hide();     var sec = 10;     $('.setthis').click(function() {         var timer = setinterval(function() {             sec = sec - 1;             if (sec > 0) {                 $('#target').click(function() {                     $('#total').html(function(i, val) {                         return val * 1 + 1                     });                 })             }             if (sec == 0) {                 $('#total').show();                 clearinterval(timer);             }         }, 1000);     })     $('#showme').click(function() {         $('#total').show();     }) }); 

every time click button, setting interval not right. should rather set interval once (on first click) , use mark end of 10 seconds.

try following:

$('#total').hide(); var clicks = 0; var timerset = false; var timerdone = false; $('#setthis').click(function () {     if (!timerset) {         timerset = true;         setinterval(function () {             timerdone = true;         }, 10000);     }     if (!timerdone) {         clicks++;         $('#total').text(clicks);     }  }); $('#showme').click(function () {     $('#total').show(); }); 

see working fiddle


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -