[英]Increment not working with setInterval
當您為程序提供種子和最大值時,它將遍歷每個數字並將其除以2和3,並顯示余數。 我希望它在解決問題的時間與下一個問題之間花費一秒鍾的時間。 但是,每當我嘗試使用setInterval
時,即使在2個不同的編輯器上,它也會凍結並崩潰。 我還有另一個問題,因為當您在輸入框中輸入種子和最大值時,它不會保存數字,而是使用javascript中的數字而不是輸入框中的數字。 預先感謝,我11歲,幾個月前開始編程。
這是我到目前為止的內容:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Made with Thimble</title>
<link rel="stylesheet" >
</head>
<body bgcolor="lightblue">
<div id="d2">
Divisible by 2
</div>
<div id="d3">
Divisible by 3
</div>
<div>
<input id="seed" type="number" placeholder="seed" value="3"><br>
<input id="max" type="number" placeholder="max" value="8">
</div>
<button onclick="count()">Count</button>
<div id="output"></div>
<script>
function count(){
var seed= document.getElementById("seed").getAttribute("value")
var max=document.getElementById("max").getAttribute("value")
var temp=1
var output=document.getElementById("output")
temp=seed
console.log("seed:"+seed)
console.log("max:"+max)
while (temp<max){
var intdivby2 = temp%2
var intdivby3 = temp%3
document.getElementById("output").innerHTML+="remainder of "+temp+" divided
by 2 is:"+intdivby2.toString()+"<br>"
document.getElementById("output").innerHTML+="remainder of "+temp+" divided
by 3 is:"+intdivby3.toString()+"<br>"
setInterval(function(){temp++;},1)
}
}
</script>
</body>
</html>
該站點崩潰是因為您在循環內調用setInterval()
。 因此,對於循環的每個周期,都將創建一個新的計時器。 這不是您想要發生的事情。
這是您需要解決的問題:
setInterval()
的第二個參數是時間(以毫秒為單位) ,而不是秒。 因此,您必須將其更改為1000。(1秒= 1000毫秒)。
刪除循環並僅調用一次setInterval()
。 在每1秒發生的功能中,增加temp
的值並執行必要的操作。
一旦temp
超過max
的值,請調用clearInterval()
停止計時器。
這是修改后的count()函數:
function count() {
var seed= document.getElementById("seed").getAttribute("value")
var max=document.getElementById("max").getAttribute("value")
var temp=1
var output=document.getElementById("output")
temp=seed
console.log("seed:"+seed)
console.log("max:"+max)
var id = setInterval(function(){
var intdivby2 = temp%2
var intdivby3 = temp%3
document.getElementById("output").innerHTML+="remainder of "+temp+" divided by 2 is:"+intdivby2.toString()+"<br>"
document.getElementById("output").innerHTML+="remainder of "+temp+" divided by 3 is:"+intdivby3.toString()+"<br>"
temp++;
if (temp > max) clearInterval(id);
},1000)
}
已經有while
循環了。不要使用setInterval
進入循環問題。最好刪除setInterval
並在while中添加temp++
。並且您的語句未正確關閉;
每行末端
更新
做直銷setInterval
來代替while
時間間隔
function count() { var seed = document.getElementById("seed").getAttribute("value") var max = document.getElementById("max").getAttribute("value") var temp = 1 var output = document.getElementById("output") temp = seed // console.log("seed:" + seed) //console.log("max:" + max) var timer = setInterval(function(){ if(temp < max) { var intdivby2 = temp % 2 var intdivby3 = temp % 3 document.getElementById("output").innerHTML += "remainder of " + temp + " divided by 2 is: " + intdivby2.toString() + " <br> " document.getElementById("output").innerHTML += "remainder of " + temp + " divided by 3 is: " + intdivby3.toString() + " <br> "; temp++ } else{ clearInterval(timer) } },500) }
<body bgcolor="lightblue"> <div id="d2"> Divisible by 2 </div> <div id="d3"> Divisible by 3 </div> <div> <input id="seed" type="number" placeholder="seed" value="3"><br> <input id="max" type="number" placeholder="max" value="8"> </div> <button onclick="count()">Count</button> <div id="output"></div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.