使用同一按钮启动和停止的 Jquery 计时器
我一生都无法弄清楚如何让它无错误地工作。
下面代码中的按钮需要做三件事。
- 单击时开始倒计时(有效)
- 自动结束倒计时,并在达到 0 时自行重置(有效)
- 如果在倒计时中间单击,则过早重置自身(有效)
错误:重复单击时会启动多个倒计时,以及或多或少的休息。如果重复单击,它需要自行重置或开始倒计时。倒计时不应超过一次。
只要人们按下按钮,等待一秒钟,然后再次按下它即可停止它,它就可以正常工作。
我遇到的错误是,如果有人垃圾邮件点击它,它会开始多次倒计时,通常只会破坏按钮。我尝试了很多不同的方法来修复它,这是我得到的最接近的方法。
var i = 29;
let running=false;
$("#startButton").click(function () {
if(running==false){
var countdown = setInterval(function () {
$("#startButton").text("Reset Timer");
running=true;
$("#stopWatch").html(i);
i--;
if (i <0)
{
$("#startButton").text("Start Timer");
running=false;
clearInterval(countdown);
i = 29;
$("#stopWatch").html(i);
}
$("#startButton").click(function () {
$("#startButton").text("Start Timer");
running=false;
clearInterval(countdown);
i = 29;
$("#stopWatch").html(i+1);
});
}, 1000);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="stopWatch">30</div>
<button id="startButton">Start Timer</button>
I can't for the life of my figure out how to get this to work bug free.
The button in the code below needs to do three things.
- Start a countdown when clicked (works)
- End the countdown automatically, and reset itself when it reaches 0(works)
- Reset itself prematurely if its clicked in the middle of a countdown(works, sort of)
Bug: when clicked repeatedly it starts multiple countdowns, and more or less breaks. It needs to either reset itself or start a countdown if clicked repeatedly. There should never be more than one countdown.
It works fines as long as people press the button, wait a second, and then press it again to stop it.
The bug I'm running into is if someone spam clicks it, it starts multiple countdowns and generally just breaks the button. I've tried a lot of different methods to fix it, and this is the closest I've gotten.
var i = 29;
let running=false;
$("#startButton").click(function () {
if(running==false){
var countdown = setInterval(function () {
$("#startButton").text("Reset Timer");
running=true;
$("#stopWatch").html(i);
i--;
if (i <0)
{
$("#startButton").text("Start Timer");
running=false;
clearInterval(countdown);
i = 29;
$("#stopWatch").html(i);
}
$("#startButton").click(function () {
$("#startButton").text("Start Timer");
running=false;
clearInterval(countdown);
i = 29;
$("#stopWatch").html(i+1);
});
}, 1000);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="stopWatch">30</div>
<button id="startButton">Start Timer</button>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
欢迎来到 Stack Overflow @William!
我不确定这意味着什么:
如果在倒计时中间单击,则提前重置自身(有点有效)
。但我设法修复了垃圾邮件按钮点击上的错误,对于第 3 项,我只是从初始状态重置了倒计时。请参阅下面的片段:在片段上添加了一些评论。如果您有任何疑问,请随时询问。
Welcome to Stack Overflow @William!
I'm not sure what this means:
Reset itself prematurely if its clicked in the middle of a countdown(works, sort of)
. But I managed to fix your bug on spamming button click and for item 3, i just do reset the countdown from initial state. See snippets below:Added some comments on the snippet. Feel free to ask if you have any questions.