简体   繁体   English

暂停倒数计时器(JavaScript)

[英]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. 如果单击重置按钮后再次启用go按钮,那也很好,但是到目前为止,只要设置了计时器,它就会继续被禁用。

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! 谢谢!

Here you have solution of working countdown: http://jsfiddle.net/XcvaE/4/ 在这里,您可以找到工作倒计时的解决方案: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM