[英]How to clear the timer in my function?
有關此問題的所有材料都在這里。
打開p1.html,雪會自動飛翔。
單擊stop button
無法停止飛行的聲音。
也許clearInterval(timer);
在stopFly函數中無法運行。
如何解決?
js的一部分。
function stopFly(){
clearInterval(timer);
document.getElementById("startButton").disabled = "";
document.getElementById("stopButton").disabled = "disabled";
}
window.onload=function(){
createManySnow();
setInterval(startFly,100);
}
html的一部分。
<input type="button" value="new" onclick="createManySnow();">
<input type="button" id="startButton" value="start" onclick="timer=setInterval(startFly,100)">
<input type="button" id="stopButton" value="stop" onclick="stopFly();">
您永遠不會在onload
函數中分配timer
變量。 因此,間隔運行時沒有指針。
window.onload = function () {
createManySnow();
window.timer = setInterval(startFly, 100);
};
您必須將創建的計時器分配給變量。
let startFlyInterval;
function stopFly(){
clearInterval(startFlyInterval);
document.getElementById("startButton").disabled = "";
document.getElementById("stopButton").disabled = "disabled";
}
window.onload = function(){
createManySnow();
startFlyInterval = setInterval(startFly, 100);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.