[英]clearInterval() function is not working in JavaScript
[英]javascript clearInterval() function is not working
var a = 0;
window.addEventListener("keydown", function(e) {
clearInterval(interval) //this dont work
var interval = setInterval(function() { //this is good
if (a == 0) {
console.log(a);
a = 10;
} else if (a == 10) {
console.log(a);
}
}, 120)
})
//我希望當用戶按下按鍵間隔停止時,新的間隔重新開始但舊的間隔不能停止
你有兩個問題。
var interval
,因此每次 function 運行時都會重置。setTimeout
將在一段時間后調用一次 function。 它不會清除間隔,您需要clearInterval
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.