• <legend id='n8APi'><style id='n8APi'><dir id='n8APi'><q id='n8APi'></q></dir></style></legend>

    <tfoot id='n8APi'></tfoot>

      1. <small id='n8APi'></small><noframes id='n8APi'>

        <i id='n8APi'><tr id='n8APi'><dt id='n8APi'><q id='n8APi'><span id='n8APi'><b id='n8APi'><form id='n8APi'><ins id='n8APi'></ins><ul id='n8APi'></ul><sub id='n8APi'></sub></form><legend id='n8APi'></legend><bdo id='n8APi'><pre id='n8APi'><center id='n8APi'></center></pre></bdo></b><th id='n8APi'></th></span></q></dt></tr></i><div id='n8APi'><tfoot id='n8APi'></tfoot><dl id='n8APi'><fieldset id='n8APi'></fieldset></dl></div>
          <bdo id='n8APi'></bdo><ul id='n8APi'></ul>

        每 2 秒获取一次调用,但不希望请求堆积

        时间:2023-10-01
        <tfoot id='ApAI1'></tfoot>

            <tbody id='ApAI1'></tbody>

            • <bdo id='ApAI1'></bdo><ul id='ApAI1'></ul>

                <i id='ApAI1'><tr id='ApAI1'><dt id='ApAI1'><q id='ApAI1'><span id='ApAI1'><b id='ApAI1'><form id='ApAI1'><ins id='ApAI1'></ins><ul id='ApAI1'></ul><sub id='ApAI1'></sub></form><legend id='ApAI1'></legend><bdo id='ApAI1'><pre id='ApAI1'><center id='ApAI1'></center></pre></bdo></b><th id='ApAI1'></th></span></q></dt></tr></i><div id='ApAI1'><tfoot id='ApAI1'></tfoot><dl id='ApAI1'><fieldset id='ApAI1'></fieldset></dl></div>

                  <small id='ApAI1'></small><noframes id='ApAI1'>

                  <legend id='ApAI1'><style id='ApAI1'><dir id='ApAI1'><q id='ApAI1'></q></dir></style></legend>
                  本文介绍了每 2 秒获取一次调用,但不希望请求堆积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试进行 API 调用,我希望它每 2 秒重复一次.但是我担心如果系统在 2 秒内没有收到请求,它会建立请求并继续尝试发送它们.我怎样才能防止这种情况发生?

                  I am trying to make an API call and I want it to repeat every 2 seconds. However I am afraid that if the system doesn't get a request back in 2 seconds, that it will build up requests and keep trying to send them. How can I prevent this?

                  这是我尝试获取的操作:

                  const getMachineAction = async () => {
                      try {
                          const response = await fetch( 'https://localhost:55620/api/machine/');
                          if (response.status === 200) {
                              console.log("Machine successfully found.");
                              const myJson = await response.json(); //extract JSON from the http response
                              console.log(myJson);               
                          } else {
                              console.log("not a 200");
                          }
                      } catch (err) {
                          // catches errors both in fetch and response.json
                          console.log(err);
                      }
                  };
                  

                  然后我用 setInterval 调用它.

                  function ping() {
                      setInterval(
                          getMachineAction(),
                          2000
                      );        
                  }
                  

                  我曾想过在 setInterval 中做一些类似结构的承诺,以确保获取已经工作并完成,但无法使其正常工作.

                  I have thought of doing some promise like structure in the setInterval to make sure that the fetch had worked and completed, but couldn't get it working.

                  推荐答案

                  Promise.all() 解决方案

                  此解决方案可确保您不会错过 2 秒延迟要求,也不会在另一个网络呼叫正在进行时触发呼叫.

                  This solution ensures that you don't miss-out on 2 sec delay requirement AND also don't fire a call when another network call is underway.

                  function callme(){
                  //This promise will resolve when the network call succeeds
                  //Feel free to make a REST fetch using promises and assign it to networkPromise
                  var networkPromise = fetch('https://jsonplaceholder.typicode.com/todos/1');
                  
                  
                  //This promise will resolve when 2 seconds have passed
                  var timeOutPromise = new Promise(function(resolve, reject) {
                    // 2 Second delay
                    setTimeout(resolve, 2000, 'Timeout Done');
                  });
                  
                  Promise.all(
                  [networkPromise, timeOutPromise]).then(function(values) {
                    console.log("Atleast 2 secs + TTL (Network/server)");
                    //Repeat
                    callme();
                  });
                  }
                  callme();

                  注意:这会按照问题作者的要求处理不良案例定义:

                  Note: This takes care of the bad case definition as requested by the author of the question:

                  坏情况"(即需要超过 2 秒)是我希望它跳过该请求,然后发送一个新请求.所以在 0 秒时请求发送.需要 3 秒执行,然后 2 秒后(在 5)它应该重新执行.所以它只是延长时间直到它发送."

                  这篇关于每 2 秒获取一次调用,但不希望请求堆积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何确定 Fetch 请求的状态码? 下一篇:Fetch API 如何获取响应体?

                  相关文章

                      <bdo id='9XNHg'></bdo><ul id='9XNHg'></ul>

                    <small id='9XNHg'></small><noframes id='9XNHg'>

                    <tfoot id='9XNHg'></tfoot>

                    <legend id='9XNHg'><style id='9XNHg'><dir id='9XNHg'><q id='9XNHg'></q></dir></style></legend>
                  1. <i id='9XNHg'><tr id='9XNHg'><dt id='9XNHg'><q id='9XNHg'><span id='9XNHg'><b id='9XNHg'><form id='9XNHg'><ins id='9XNHg'></ins><ul id='9XNHg'></ul><sub id='9XNHg'></sub></form><legend id='9XNHg'></legend><bdo id='9XNHg'><pre id='9XNHg'><center id='9XNHg'></center></pre></bdo></b><th id='9XNHg'></th></span></q></dt></tr></i><div id='9XNHg'><tfoot id='9XNHg'></tfoot><dl id='9XNHg'><fieldset id='9XNHg'></fieldset></dl></div>