gpt4 book ai didi

javascript - 清除间隔不起作用

转载 作者:行者123 更新时间:2023-11-28 13:19:21 24 4
gpt4 key购买 nike

var button1 = document.getElementById("start");
var button2 = document.getElementById("stop");
var timegraph = document.getElementById("ceas");
var time = 0;
var ResetStart = 0;
function Start(){
if (ResetStart==0) {
ResetStart=1;
Running();
button1.innerHTML="Pause";
}
else {
ResetStart=0;
button1.innerHTML="Resume";
clearInterval(myInterval);
}
}
function Reset(){
time = 0;
ResetStart = 0;
button1.innerHTML="Start";
timegraph.innerHTML="00:00:00:00";
}
function OnGoing(){
time++;
var hours = Math.floor(time/100/60/60);
var minutes = Math.floor(time/100/60 % 60);
var seconds = Math.floor(time/100 % 60);
var hundreds = Math.floor(time/10 % 10);
var thousands = time % 10;
if (hours<10){
hours = "0" + hours;
}
if (minutes<10) {
minutes = "0" + minutes;
}
if (seconds<10) {
seconds = "0" + seconds;
}
timegraph.innerHTML=hours + ":" + minutes + ":" + seconds + ":" + hundreds + thousands;
}
function Running(){
if (ResetStart==1){
var myInterval = setInterval (OnGoing , 10);
}
else {
timegraph.innerHTML="00:00:00:00";
}
}

这段代码应该是一个秒表。问题是clearInterval 不起作用。当我第二次按下 id:“start”的按钮时,控制台中出现的错误是:Uncaught ReferenceError:myInterval 未定义。当我第二次按下该按钮时,代码秒表应该停止。

最佳答案

这是因为 myInterval 是在 Running 下定义的。删除 var 并将其定义在 Running 之外。

关于javascript - 清除间隔不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34754075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com