I'm trying to make a game for my kids that relies on a counter. I have the counter working fine, except on the third play-throughthe counter doubles in speed.
The first two plays the counter increments 1 second at a time, then the third round it increments two seconds at a time, then the fourth round it increments four seconds at a time, and so on...
My code:
timeLeft = {
total: gameTime,
mins: function(){
return Math.floor(timeLeft.total/60);
},
secs: function(){
tempSecs=timeLeft.total-(60*timeLeft.mins());
if (tempSecs < 10) {
tempSecs='0'+tempSecs;
}
return tempSecs;
}
};
function timer(){
$('#time').html(timeLeft.mins() + " : " + timeLeft.secs());
timeLeft.total=timeLeft.total-1;
if (timeLeft.total>=0) {
setTimeout(function(){
timer()}, 1000);
}
}
};
Set your timeout to a variable: var myTimeout = setTimeout... and when you reset the game, run a clearTimeout(myTimeout);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With