javascript 带暂停/重置的倒数计时器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7325146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:44:38  来源:igfitidea点击:

Countdown timer with pause/reset

javascripttimercountdown

提问by Samantha

I'm looking for an open source JavaScript timer that will countdown from 60 seconds. I would prefer if people could increase the timing on the timer, but that's not necessary. It is necessary for people to be able to start and pause the time and reset it when they need to.

我正在寻找一个可以从 60 秒开始倒计时的开源 JavaScript 计时器。我希望人们可以增加计时器的计时,但这不是必需的。人们需要能够开始和暂停时间并在需要时重新设置时间。

A tutorial or source code would be great.

教程或源代码会很棒。

回答by etuardu

Here is a solution in pure javascript to get you started: http://jsfiddle.net/XcvaE/4/

这是一个纯 javascript 的解决方案,可以帮助您入门:http: //jsfiddle.net/XcvaE/4/

回答by kaleazy

This should give you a good start. This timer will fire a "Hello" alertbox after 30 seconds. However, everytime you click the reset timer button it clears the timerHandle and then re-sets it again. Once it's fired, the game ends.

这应该给你一个良好的开端。此计时器将在 30 秒后触发“您好”警报框。但是,每次单击重置计时器按钮时,它都会清除 timerHandle,然后再次重新设置它。一旦发射,游戏结束。

<script type="text/javascript">
    var timerHandle = setTimeout("alert('Hello')",3000);
    function resetTimer() {
        window.clearTimeout(timerHandle);
        timerHandle = setTimeout("alert('Hello')",3000);
    }
</script>

<body>
    <button onclick="resetTimer()">Reset Timer</button>
</body>