3 回答

TA贡献1836条经验 获得超5个赞
使用while(true)没有问题。你读到的“不”是因为它是一个可能导致无限循环的潜在问题。
因此,在这种情况下,如果在此之前没有任何反应,您可以使用计数器来中断循环。例如,max_attempt = 100。

TA贡献1859条经验 获得超6个赞
可以像这样更简单,举个例子:
var x = 0;
console.log("Entering loop");
while(true){
// always make sure to update something in your condition
// so you dont fall into an infinite loop
x++;
console.log(x); // mostly demostrative
if(x===3) break; // condition to determine when should the loop stop
}
console.log("Out of the loop");

TA贡献1790条经验 获得超9个赞
为什么不这样做:
for(get some data; condition based on data above; get some data;) {
do something;
}
例如:
for(var i = Math.random(); i < .8; i = Math.random()) {
console.log(i);
}
添加回答
举报