且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何在While循环中使用访存

更新时间:2023-01-03 19:22:21

while(true)创建一个无限循环,该循环将尝试调用 fetch 无数次在一个单个刻度" 中.由于它永远不会完成发出新的 fetch 调用,因此永远不会到达下一个刻度.

while(true) creates an infinite loop, which will attempt to call fetch infinitely many times within a single "tick". Since it never finishes issuing new fetch calls, it never gets to the next tick.

此功能占用大量CPU,可能会锁定整个页面.

This function is very CPU-intensive and will probably lock up the entire page.

有什么解决方案?

您可能要尝试做的是继续获取​​,直到结果满足某些条件为止.您可以通过检查 then 回调中的条件并重新发出 false (如果为 false :

What you are probably trying to do is keep fetching until the result satisfies some condition. You can achieve that by checking the condition in the then callback, and re-issuing the fetch if it is false:

var resultFound = false;

var fetchNow = function() {
  fetch('some/address').then(function() {
    if(someCondition) {
      resultFound = true;
    }
    else {
      fetchNow();
    }
  });
}

fetchNow();

这样,而不是

fetch!
fetch!
fetch!
fetch!
...

...行为将会发生

fetch!
  wait for response
  check condition
if false, fetch!
  wait for response
  check condition
if true, stop.

...这可能是您所期望的.

...which is probably what you expected.