如何让以下for循环语句不止一次通过我的数组?
var clients = new Array("client1", "client2", "client3", "client4", "client5");
var indexCounter;
var holmesminutes = 0;
for (indexCounter = 0; indexCounter < clients.length; indexCounter++) {
holmesminutes = holmesminutes + 15;
document.write(clients[indexCounter] + " testroom " + holmesminutes +"<br>");
}
答案 0 :(得分:2)
在循环周围放置另一个循环。
for (var repeatCounter = 0; repeatCounter < 5; repeatCounter++) {
for (indexCounter = 0; indexCounter < clients.length; indexCounter++) {
holmesminutes = holmesminutes + 15;
document.write(clients[indexCounter] + " testroom " + holmesminutes +"<br>");
}
}
在holmesminutes
到达315时停止所有循环:
for (var repeatCounter = 0; repeatCounter < 5 && holmesminutes < 315; repeatCounter++) {
for (indexCounter = 0; indexCounter < clients.length && holmesminutes < 315; indexCounter++) {
holmesminutes = holmesminutes + 15;
document.write(clients[indexCounter] + " testroom " + holmesminutes +"<br>");
}
}
如您所见,您可以将任何条件放在for
的test子句中,它不必仅引用迭代变量。
答案 1 :(得分:1)
也许试试while loop:
var clients = new Array("client1","client2","client3","client4","client5");
var indexCounter;
var holmesminutes =0;
_i= 0;
while (_i < 2) {
for(indexCounter = 0; indexCounter<clients.length;indexCounter++)
{
holmesminutes = holmesminutes + 15;
document.write(clients[indexCounter] + " testroom " + holmesminutes + "<br>");
_i++;
}
}