regniscire
regniscire

Reputation: 117

JavaScript timer won't reset

I really need some help with my Javascript countdown timer. I am trying to make a countdown timer, for a quiz game, that resets and starts over when a button is pressed.

I can get it to reset and start over but if I press the button before time gets to 0, it will count down super fast.

Can anyone help? Here's the code:

<script type="text/javascript">

var countdown;
var time;

function init() {
    time = 11;
    reset();
    trigger();

}

function trigger() {

    if(time > 0) {
        time--;
        document.getElementById('timer').innerHTML = time;
        if(time > 0) {
            countdown = setTimeout('trigger()', 1000);
        }
    }
}

function reset() {

    clearTimeout(trigger);

}

</script>

<input type="button" value="Reset" onclick="init()" />

<h3 id="timer">10</h3>

Of course I have the basic HTML document set up. The script is inside the tags and there's no difference in timer behavior if I place the script in element.

Thank you in advance!

Upvotes: 1

Views: 332

Answers (1)

Billy
Billy

Reputation: 2448

You're not clearing the timeout in the reset. it should be

 clearTimeout(countdown);

like this fiddle

Upvotes: 1

Related Questions