在“holmesminutes”变量达到一定量后,如何在下面的for循环中执行if语句,例如60然后一旦达到60,它就会跳过其中一个数组项。所以例如在60分钟之后,循环应该在Peacock上的数组项目,但我希望它跳过它并将“Scarlett”放60分钟。然后,我需要在100分钟后再次这样做。
document.write('<table id="myTable1" border="1" cellspacing="1" cellpadding="5">');
var clients = new Array("Mustard","Plum","Green","Peacock","Scarlett","White");
var indexCounter,
holmesminutes =0,
tableId1 =0;
for (var repeatCounter = 0;
repeatCounter < 5 && holmesminutes < 315; repeatCounter++)
{
for (indexCounter = 0; holmesminutes < 315; indexCounter++)
{
document.write("<tr>");
tableId1 = tableId1 + 1;
document.write('<td id="' + tableId1 + '">'
+ clients[indexCounter] + '</td>');
document.write("<td> Holmes </td>");
holmesminutes = holmesminutes + 15;
document.write("<td>" + holmesminutes + "</td> </tr>");
}
}
document.write("</table> <br>");
答案 0 :(得分:0)
如果您的控制变量是holmesminutes,那么理解您的代码要简单得多。
如果您需要在100之后跳过一次,只需跳过105.
indexCounter = 0;
MAX_CLIENTS = clients.length;
for (var holmesminutes = 0; holmesminutes < 315; holmesminutes += 15)
{
if (holmesminutes != 60 && holmesminutes != 105) {
tableId1 = tableId1 + 1;
document.write("<tr>");
document.write('<td id="' + tableId1 + '">' +
clients[indexCounter] + '</td>');
document.write("<td> Holmes </td>");
document.write("<td>" + holmesminutes + "</td> </tr>");
}
indexCounter += 1;
if (indexCounter == MAX_CLIENTS) indexCounter = 0;
}
我不确定您是否要在显示所有客户端或想要重新开始时退出。如果您想退出,只需更改为if (indexCounter == MAX_CLIENTS) break;
答案 1 :(得分:0)
像这样使用breaks
;
<script>
var counter = 0, full_breakpoint = 15, inner_break = 8;
for(var a = 0; a<10; ++a){
for(var b = 0; b<10; ++b){
counter++;
if(counter >= full_breakpoint) {
console.log("full_breakpoint in <b>loop = " + counter);
break;
}else if(counter == inner_break){
console.log("inner_break in <b>loop = " + counter);
break;
}else console.log("no break = " + counter);
}
if(counter >= full_breakpoint){
console.log("outter <a> forloop at break_point = " + counter);
break;
}
}
console.log("finish " + counter);
</script>
输出;
no break = 1 javascript.html:16
no break = 2 javascript.html:16
no break = 3 javascript.html:16
no break = 4 javascript.html:16
no break = 5 javascript.html:16
no break = 6 javascript.html:16
no break = 7 javascript.html:16
inner_break in <b>loop = 8 javascript.html:14
no break = 9 javascript.html:16
no break = 10 javascript.html:16
no break = 11 javascript.html:16
no break = 12 javascript.html:16
no break = 13 javascript.html:16
no break = 14 javascript.html:16
full_breakpoint in <b>loop = 15 javascript.html:11
outter <a> forloop at break_point = 15 javascript.html:22
finish 15