我在Javascript中创建了一个每6秒更改一次的切换声明..但是当它进入情况10时它会停止。我希望它从案例1重新开始。请问任何代码?
这是我的间隔:
var numri = 1;
var itr = setInterval(numrat, 6000);
function numrat() {
switch (numri) {
case 10:
document.getElementById("td10").innerHTML = 'x5000';
document.getElementById("td9").innerHTML = 'x1000';
document.getElementById("td8").innerHTML = 'x300';
document.getElementById("td7").innerHTML = 'x30';
document.getElementById("td6").innerHTML = 'x10';
document.getElementById("td5").innerHTML = 'x5';
document.getElementById("td4").innerHTML = 'x2';
document.getElementById("td3").innerHTML = '-';
document.getElementById("td2").innerHTML = '-';
document.getElementById("td1").innerHTML = '-';
numri++;
break;
case 1:
document.getElementById("td1").innerHTML = 'x3';
numri++;
break;
}
答案 0 :(得分:0)
您可以使用numri++;
替换case 10:
中的numri = 1;
:
function numrat() {
switch (numri) {
case 10:
// your logic
numri = 1;
break;
case 1:
document.getElementById("td1").innerHTML = 'x3';
numri++;
break;
}
}
解释解决方案的简化代码段:
function numrat() {
switch (numri) {
case 5:
console.log(5);
numri = 1;
break;
case 4:
case 3:
case 2:
case 1:
console.log(numri);
numri++;
break;
}
}
var numri = 1;
var itr = setInterval(numrat, 100);
// Guarding for infinite loop purposes
setTimeout(function() {
clearInterval(itr)
}, 1500);