[英]Start/stop setTimeout with a button & prevent counting faster with more clicks
I am working with JavaScript and I use a setTimeout function in order to count up. 我正在使用JavaScript,我使用setTimeout函数来计算。 Here is my code... 这是我的代码......
<button id="star">Start</button>
<p id="time">0</p>
var timeEl = 0;
function start() {
time();
}
function time() {
setTimeout(function() {
timeEl = timeEl + .1;
timeEnd = timeEl.toFixed(1);
document.getElementById("time").innerHTML = timeEnd;
time();
}, 100);
}
var el = document.getElementById("star");
el.addEventListener("click", star, false);
How do I get my setTimeout function to start on stop when I click on the button 当我单击按钮时,如何让setTimeout函数在停止时启动
How to prevent my counting from going faster the more times I click on the button. 如何防止我的计数越快,我点击按钮的次数越多。
I have included my JSFiddle below! 我在下面包含了我的JSFiddle! https://jsfiddle.net/pb4759jh68/0618eLoe/ https://jsfiddle.net/pb4759jh68/0618eLoe/
To stop a timer you can use clearTimeout()
, but it does require the id of the timer created with setTimeout()
. 要停止计时器,可以使用clearTimeout()
,但它确实需要使用setTimeout()
创建的计时器的id。 The call to setTimeout()
now saves the timer id in timeOut
and then checks the contents of timeOut
in start()
to see whether a timer is currently running. 对setTimeout()
的调用现在在timeOut
保存计时器id,然后在start()
检查timeOut
的内容以查看计时器当前是否正在运行。 If a timer is running then timeOut
is used in clearTimeout(timeOut);
如果定时器正在运行则timeOut
被使用clearTimeout(timeOut);
. 。
var timeEl = 0;
var timeOut = null;
function start()
{
if(timeOut !== null){
clearTimeout(timeOut);
timeOut = null;
}else{
time();
}
}
function time()
{
timeOut = setTimeout(function()
{
timeEl = timeEl + .1;
timeEnd = timeEl.toFixed(1);
document.getElementById("time").innerHTML = timeEnd;
time();
}, 100);
}
var el = document.getElementById("star");
el.addEventListener("click", start, false);
I hope this code clears the issue 我希望这段代码可以解决这个问题
The same can be achieved using setInterval and clearInterval. 使用setInterval和clearInterval可以实现相同的目的。 Try this JSFiddle 试试这个JSFiddle
Every time the button is pressed, you get a second copy of your timer running, which is advancing your time faster. 每次按下按钮,您都会获得计时器运行的第二个副本,这样可以更快地延长您的时间。
var el = document.getElementById("star");
el.addEventListener("click", start, false);
I would recommend something like this: 我会推荐这样的东西:
var timerId = 0;
var timeEl = 0;
function start()
{
time();
}
function time()
{
timerId = setTimeout(function()
{
timeEl = timeEl + .1;
timeEnd = timeEl.toFixed(1);
document.getElementById("time").innerHTML = timeEnd;
time();
}, 100);
}
var el = document.getElementById("star");
el.addEventListener("click", function() {
if (timerId !== 0) {
clearTimeout(timerID);
timerId = 0;
} else {
start();
}
}, false);
您可以使用clearTimeout
取消之前设置的超时 - 请参阅https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/clearTimeout 。
try this JsFiddle 试试这个JsFiddle
var timeEl = 0,timer;
function start()
{
time();
}
function time()
{
document.getElementById("star").disabled=true;
timer=setTimeout(function()
{
timeEl = timeEl + .1;
timeEnd = timeEl.toFixed(1);
document.getElementById("time").innerHTML = timeEnd;
time();
}, 100);
}
function stop(){
document.getElementById("star").disabled=false;
clearTimeout(timer);
}
var el = document.getElementById("star");
el.addEventListener("click", start, false);
var elstop = document.getElementById("stop");
elstop.addEventListener("click", stop, false);
I simply added a boolean that states if the counting is already running : 我只是添加了一个布尔值,表明计数是否已经在运行:
https://jsfiddle.net/0618eLoe/3/ https://jsfiddle.net/0618eLoe/3/
var timeEl = 0;
var isStarted = false;
function startStop()
{
if (!isStarted) {
isStarted = true;
time();
} else {
isStarted = false;
}
}
function time()
{
if (!isStarted) return;
setTimeout(function()
{
timeEl = timeEl + .1;
timeEnd = timeEl.toFixed(1);
document.getElementById("time").innerHTML = timeEnd;
time();
}, 100);
}
var el = document.getElementById("star");
el.addEventListener("click", startStop, false);
var timeEl = 0,g; function start() { if(g){ clearTimeout(g); timeEl = 0; } time(); } function time() { g=setTimeout(function() { timeEl = timeEl + .1; timeEnd = timeEl.toFixed(1); document.getElementById("time").innerHTML = timeEnd; time(); }, 100); } var el = document.getElementById("star"); el.addEventListener("click", start, false);
tell me what else would you need. or it implements your specification. here button click will start the timer from 0 again.
The following is a simple solution using setInterval
and clearInterval
以下是使用setInterval
和clearInterval
的简单解决方案
var time = 0.0;
var view = document.getElementById('time');
var running = false;
var repeat;
function start(){
if(running){
return;
}
running = true;
repeat = setInterval(increment, 100);
}
function stop(){
clearInterval(repeat);
running = false;
}
function increment(){
time += 0.1;
view.innerText = time.toFixed(1);
}
Demo: 演示:
https://jsfiddle.net/m7bmc2uj/ https://jsfiddle.net/m7bmc2uj/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.