Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for a Way to Reset the `Math` Function

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);
        }
    }
};
like image 359
freginold Avatar asked Mar 24 '26 22:03

freginold


1 Answers

Set your timeout to a variable: var myTimeout = setTimeout... and when you reset the game, run a clearTimeout(myTimeout);

like image 138
cchambers Avatar answered Mar 27 '26 10:03

cchambers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!