久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

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

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

        <tfoot id='zyDZz'></tfoot>
        • <bdo id='zyDZz'></bdo><ul id='zyDZz'></ul>

        依次調(diào)用 API 直到響應(yīng)為空

        Call API sequentially until response is empty(依次調(diào)用 API 直到響應(yīng)為空)
          <tfoot id='BB3iC'></tfoot>
            • <bdo id='BB3iC'></bdo><ul id='BB3iC'></ul>
              <legend id='BB3iC'><style id='BB3iC'><dir id='BB3iC'><q id='BB3iC'></q></dir></style></legend>
                <tbody id='BB3iC'></tbody>
            • <small id='BB3iC'></small><noframes id='BB3iC'>

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

                1. 本文介紹了依次調(diào)用 API 直到響應(yīng)為空的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我正在嘗試連續(xù)調(diào)用 API 并計(jì)數(shù)頁(yè)面,直到響應(yīng)為空.每個(gè)頁(yè)面最多返回 1000 個(gè)結(jié)果",直到最終只返回 [].

                  I'm trying to call an API continuously and count through the pages until the response is empty. Each page returns a maximum of 1000 'results' until eventually returning only [].

                  我嘗試過下面的代碼,但 while 循環(huán)無限期地繼續(xù),并且標(biāo)志永遠(yuǎn)不會(huì)設(shè)置為 false,盡管我知道第 5 頁(yè)返回空.

                  I've had an attempt at the below code but the while loop continues indefinitely and the flag is never set to false, despite the fact that I know page 5 returns empty.

                   var count = 1;
                   var flag = true;
                   var request = new XMLHttpRequest();
                  
                   while (flag == true) {
                      request.open('GET', 'https://api.example.net/results/?page=' + count, true);
                      count++;
                      request.onload = function () {
                        var data = JSON.parse(this.response);
                        if (data.length == 0) {
                          flag = false;
                        }
                      }
                      request.send();
                   }
                  

                  推薦答案

                  問題在于代碼是異步.特別是,onload 回調(diào)僅在收到響應(yīng)時(shí)觸發(fā),在調(diào)用之后的某個(gè)時(shí)間.您的腳本不會(huì)停止運(yùn)行以等待"響應(yīng),因此它會(huì)繼續(xù)遍歷循環(huán),因?yàn)?flag 仍然是 true.當(dāng)空"響應(yīng)出現(xiàn)并且 flag 變?yōu)?false 時(shí),循環(huán)可能已經(jīng)運(yùn)行了數(shù)千次,從而設(shè)置了無用"的 Ajax 請(qǐng)求.

                  The problem comes from the fact that the code is asynchronous. In particular, the onload callback only fires when the response is received, some time after the call is made. Your script doesn't stop running to "wait" for the response, so it continues ploughing through the loop, because flag is still true. By the time an "empty" response comes and flag becomes false, the loop will have run potentially thousands of times, setting up "useless" Ajax requests.

                  @AmitJoki 已經(jīng)建議了如何解決這個(gè)問題.這是一種方法(使用@PranavCBalan 建議的遞歸 - 盡管當(dāng)我看到他的評(píng)論時(shí)我已經(jīng)開始寫這個(gè)了:-)):

                  @AmitJoki already suggested how to fix this. Here is one way to do it (using recursion as suggested by @PranavCBalan - although I had already started writing this when I saw his comment :-) ):

                  function sendRequest(count) {
                      var request = new XMLHttpRequest();
                      request.open('GET', 'https://api.example.net/results/?page=' + count, true);
                      request.onload = function () {
                        var data = JSON.parse(this.response);
                        if (data.length > 0) {
                          sendRequest(count+1);
                        }
                      }
                      request.send();
                  }
                  
                  sendRequest(1);
                  

                  關(guān)鍵區(qū)別在于,在發(fā)送一個(gè)請(qǐng)求后,此代碼不會(huì)發(fā)送另一個(gè)請(qǐng)求,直到響應(yīng)返回并確認(rèn) data.length 大于 0.

                  The key difference is that, after sending one request, this code won't send another one until the response is back and confirmed to have data.length greater than 0.

                  這篇關(guān)于依次調(diào)用 API 直到響應(yīng)為空的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調(diào)用 abort (jQuery) 之后,瀏覽器也會(huì)等待 ajax 調(diào)用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請(qǐng)求的資源上不存在“Access-Control-Allow-Origin標(biāo)頭) - IT屋-程序員軟件開發(fā)技術(shù)分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請(qǐng)求是否有可能不遵循重定向 (301 302))
                  NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 異常 101)
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內(nèi)容)
                  1. <legend id='ziPWI'><style id='ziPWI'><dir id='ziPWI'><q id='ziPWI'></q></dir></style></legend>
                      <tbody id='ziPWI'></tbody>

                      <tfoot id='ziPWI'></tfoot>

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

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

                            主站蜘蛛池模板: 91午夜在线| 久久精品久久久久久 | 二区国产 | 国产成人精品综合 | 天天影视综合 | 日日骚av | 国产福利在线看 | 亚洲精品久久久一区二区三区 | 国产精品乱码一区二区三区 | 国产999精品久久久 午夜天堂精品久久久久 | 日本粉嫩一区二区三区视频 | 极品在线 | 亚洲精品视频免费 | 久久久精品一区二区三区四季av | 国产激情第一页 | 啪啪综合网 | 日韩一区二区三区精品 | 欧美福利网站 | 亚洲欧美日韩电影 | 一级毛片大全免费播放 | 国产91在线精品 | 成人1区2区 | 国产精品免费在线 | 免费人成在线观看网站 | 亚洲一区二区三区四区视频 | 精精精精xxxx免费视频 | 91精品导航 | 日本一区二区三区四区 | 一级视频在线免费观看 | 国产99久久 | 精品国产一区二区三区免费 | 免费在线观看一区二区三区 | 精品欧美一区二区在线观看视频 | 日韩精品一区二区三区在线播放 | 国产清纯白嫩初高生在线播放视频 | 欧美日韩大片 | 色综合欧美 | 亚洲精品电影网在线观看 | 99久久婷婷国产精品综合 | 精品欧美一区二区三区久久久 | 激情欧美日韩一区二区 |