JS: 当设置了超时时间时,do-while循环会无限循环执行函数。

huangapple go评论83阅读模式
英文:

JS: do-while loop infinitely loops over function when timeout is set

问题

我需要一个提示窗口,让用户输入一个数字。当设置了下面的代码后,一切都正常,但是这个提示窗口应该一直出现,直到一个以200为起点的整数变量达到0,所以我认为使用do while循环可以解决问题。但是当设置了do while循环后,提示窗口立即出现,但并没有改变任何内容。我还改变了代码,直到循环变得无限,导致我的浏览器崩溃。我知道do while循环中的条件始终为真(machineFuel > 0 && height > 0),因为变量machineFuelheight大于0,但我不知道如何正确设置它。

以下是相关的代码(已缩短):


var machineFuel = 200;
var height = 500;

setTimeout(round, 2000);

function round() {
    do {
       playerInput = input();
    } while (machineFuel > 0 && height > 0);
    calculate();
}

function input(fuel) {
    fuel = parseInt(prompt("Fuel", "A number from 0-200"));
    return fuel;
}

function calculate() {
    machineFuel = machineFuel - playerInput;
}


英文:

i need a prompt window to let the user type in a number.
When setting the function (code below) once, everything works but this promt window should accure until a variable with an integer (starting from 200) reaches 0, so i thought a do while loop would do the trick. <br>
But when setting the do while loop the prompt window shows up immediately after typing in a number and does not change anything. <br>
I also changed the code until the loop got infinite and my brwoser crashed.
I know that the condition in the do while loop is always true (machineFuel &gt; 0 &amp;&amp; height &gt; 0) because the variables machineFuel and height are greater than 0 but i dont know how to set it up correctly.

Here the relevant code (shortened)


var machineFuel = 200;
var height = 500;


setTimeout(round, 2000);

function round() {
    do {
       playerInput = input();
    } while (machineFuel &gt; 0 &amp;&amp; height &gt; 0);
    calculate();
}

function input(fuel) {
    fuel = parseInt(prompt(&quot;Fuel&quot;, &quot;A number from 0-200&quot;));
    return fuel;
}

function calculate() {
    machineFuel = machineFuel - playerInput;
}


答案1

得分: 2

calculate移动到input()之后,否则你的machineFuel不会被更新,while循环将永远运行。
此外,OP在评论中提到他需要在每个提示后设置超时。因此,我们移除while循环,并再次调用setTimeout()

var machineFuel = 200;
var height = 500;

setTimeout(round, 2000);

function round() {
   playerInput = input();
   calculate();
   machineFuel > 0 && height > 0 && setTimeout(round, 2000);
}

function input(fuel) {
    fuel = parseInt(prompt("Fuel", "A number from 0-200"));
    return fuel;
}

function calculate() {
    machineFuel = machineFuel - playerInput;
}

OP要求添加一个while选项。为此,我们需要将setTimeout包装到一个Promise中:

var machineFuel = 200;
var height = 500;

round();

async function round() {

  do {
    
   await delay(2000);
    
   playerInput = input();
   calculate();
  
  } while (machineFuel > 0 && height > 0);

}

function delay(delay) {
    return new Promise(function(resolve){
      setTimeout(resolve, delay);
    });
}

function input(fuel) {
    fuel = parseInt(prompt("Fuel", "A number from 0-200"));
    return fuel;
}

function calculate() {
    machineFuel = machineFuel - playerInput;
}
英文:

Move calculate after input() otherwise your machineFuel won't be updated and while will run forever.
Also the OP commented he needs the timeout after each prompt. So we remove while and call setTimeout() again:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var machineFuel = 200;
var height = 500;


setTimeout(round, 2000);

function round() {
   playerInput = input();
   calculate();
   machineFuel &gt; 0 &amp;&amp; height &gt; 0 &amp;&amp; setTimeout(round, 2000);
}

function input(fuel) {
    fuel = parseInt(prompt(&quot;Fuel&quot;, &quot;A number from 0-200&quot;));
    return fuel;
}

function calculate() {
    machineFuel = machineFuel - playerInput;
}

<!-- end snippet -->

The OP asked for a while option. For that we need to wrap setTimeout to a promise:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var machineFuel = 200;
var height = 500;

round();

async function round() {

  do {
    
   await delay(2000);
    
   playerInput = input();
   calculate();
  
  } while (machineFuel &gt; 0 &amp;&amp; height &gt; 0);

}

function delay(delay) {
    return new Promise(function(resolve){
      setTimeout(resolve, delay);
    });
}

function input(fuel) {
    fuel = parseInt(prompt(&quot;Fuel&quot;, &quot;A number from 0-200&quot;));
    return fuel;
}

function calculate() {
    machineFuel = machineFuel - playerInput;
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月27日 15:50:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定