0
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未定義。當我按下第二次按鈕時,代碼秒錶應該停止。clearinterval不起作用