nonono
nonono

Reputation: 601

Pause countdown timer (JavaScript)

I want the countdown timer to pause when I click the pause button, and start from where it left off when I click the go button. It'd also be good if the go button enables again when reset is clicked, but as of now it continues being disabled for as long as the timer was set.

For pausing, I tried what I saw in this fiddle but it didn't work. I also tried this to no avail:

$("#pause").click(function() {
   clearInterval(count()); 
})

Here's the fiddle.

Thanks!

Upvotes: 0

Views: 379

Answers (1)

galethil
galethil

Reputation: 1016

Here you have solution of working countdown: http://jsfiddle.net/XcvaE/4/

<script>

    var CCOUNT = 60;

    var t, count;

    function cddisplay() {
        // displays time in span
        document.getElementById('timespan').innerHTML = count;
    };

    function countdown() {
        // starts countdown
        cddisplay();
        if (count == 0) {
            // time is up
        } else {
            count--;
            t = setTimeout("countdown()", 1000);
        }
    };

    function cdpause() {
        // pauses countdown
        clearTimeout(t);
    };

    function cdreset() {
        // resets countdown
        cdpause();
        count = CCOUNT;
        cddisplay();
    };

</script>

<body onload="cdreset()">
<span id="timespan"></span>
<input type="button" value="Start" onclick="countdown()">
<input type="button" value="Stop" onclick="cdpause()">
<input type="button" value="Reset" onclick="cdreset()">
</body>

Upvotes: 1

Related Questions