我需要我的 JavaScript 计时器停在 0,显示一条消息,然后重新启动
我创建了一个 JavaScript 计时器,从 40 开始倒计时 1,但它不会在 0 处停止,我希望它在达到 0 时显示一条消息(不是警报)/向数据库添加一个项目,然后重新启动40秒时。如果您可以提供任何帮助,请提前致谢:D
<html>
<head>
<script type="text/javascript">
var c=40;
var t;
var timer_is_on=0;
function timedCount()
{
document.getElementById('txt').value=c;
c=c-1;
t=setTimeout("timedCount()",1000);
}
function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
timedCount();
}
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onClick="doTimer()">
<input type="text" id="txt">
</form>
<p>Click on the button above. It will count down from 40</p>
</body>
</html>
I have created a javascript timer that starts at 40 and counts down by one, but it wont stop at 0 and i want it to display a message(not an alert)/add an item to a database when it hits 0, and then restart at 40 seconds. If you could provide any help, thanks in advance :D
<html>
<head>
<script type="text/javascript">
var c=40;
var t;
var timer_is_on=0;
function timedCount()
{
document.getElementById('txt').value=c;
c=c-1;
t=setTimeout("timedCount()",1000);
}
function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
timedCount();
}
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onClick="doTimer()">
<input type="text" id="txt">
</form>
<p>Click on the button above. It will count down from 40</p>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要使用 setInterval 而不是 setTimeout,因为这将根据您指定的延迟定期触发该方法。
当计数达到 0 时,这将重新启动计时器:
如果您想在计数达到 0 时停止计时器,请使用以下命令:
You want to use setInterval rather than setTimeout as this will fire the method periodically according to the delay you specify.
This will restart the timer when the count hits 0:
If you want to stop the timer when the count hits 0 use this: