簡體   English   中英

如何清除我的計時器功能?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM