最简单的 React-native setTimeout 不等待

Simplest React-native setTimeout not waiting

我需要延迟获取函数的执行,但我什至无法使简单的 setTimeout 工作!

 const delay = ms => new Promise(res => setTimeout(res, ms));


    while (true) {
      await delay(10000)
      console.log(new Date())  
    }

console.log的

 LOG  2021-05-04T08:36:18.833Z
 LOG  2021-05-04T08:36:21.598Z
 LOG  2021-05-04T08:36:21.615Z
 LOG  2021-05-04T08:36:21.618Z
 LOG  2021-05-04T08:36:22.665Z
 LOG  2021-05-04T08:36:22.667Z

这个怎么样:

let fetchTimer;

const delayedFetch = () => {
    clearTimeout(fetchTimer);

    fetchTimer = setTimeout(() => {                   
        doFetch();
        clearTimeout(fetchTimer);
    }, 10000);
}

setTimeout、setInterval 和计时器通常不起作用的原因是因为我从 App.js 中多次安装了我的组件。发生这种情况是因为我使用了异步 useState,没有意识到它重新渲染了我的所有组件并以某种方式扰乱了计时器。

我也可能是因为我处于调试模式并且调试器和应用程序之间存在偏移