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

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

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

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

      1. <legend id='ewFTI'><style id='ewFTI'><dir id='ewFTI'><q id='ewFTI'></q></dir></style></legend>

        如何處理對后端服務進行相同調用的多個瀏覽器

        How do I handle multiple browser scripts making the same calls to the back-end service(如何處理對后端服務進行相同調用的多個瀏覽器腳本)
          <tbody id='D2VGR'></tbody>

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

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

              <legend id='D2VGR'><style id='D2VGR'><dir id='D2VGR'><q id='D2VGR'></q></dir></style></legend>
                1. 本文介紹了如何處理對后端服務進行相同調用的多個瀏覽器腳本的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有一個網頁,其中的不同部分都需要相同的后端數據.每個都是孤立的,因此它們最終都會對后端進行相同的調用.

                  I have a web page where different parts of it all need the same back-end data. Each is isolated, so they each end up eventually making the same calls to the back-end.

                  當調用已經在進行中并由同一網頁上的不同代碼段啟動時,避免調用 Web 服務器的最佳方法是什么?

                  What is the best way to avoid making a call to the web server when one is already in progress and initiated by a different piece of code on the same web page?

                  這是一個例子.我將使用 setTimeout 來模擬異步調用.

                  Here's an example. I'll use setTimeout to simulate an asynchronous call.

                  假設有一個異步函數返回聯系人列表,在這個例子中它基本上是一個簡單的字符串數組:

                  Let's assume there's an async function that returns the list of contacts, which is basically a simple array of strings in this example:

                  var getContacts = function() {
                    log('Calling back-end to get contact list.');
                    return new Promise(function(resolve, reject) {
                      setTimeout(function() {
                        log('New data received from back-end.');
                        resolve(["Mary","Frank","Klaus"]);
                      }, 3000);
                    });
                  };
                  

                  現在,讓我們創建三個單獨的函數,每個函數都出于不同的目的調用上述函數.

                  Now, let's create three separate functions that each call the above function for different purposes.

                  轉儲聯系人列表:

                  var dumpContacts = function() {
                    getContacts().then(function(contacts) {
                      for( var i = 0; i < contacts.length; i++ ) {
                        log( "Contact " + (i + 1) + ": " + contacts[i] );
                      }
                    });
                  };
                  

                  確定特定聯系人是否在列表中:

                  Determine if a particular contact is in the list:

                  var contactExists = function(contactName) {
                    return getContacts().then(function(contacts) {
                      return contacts.indexOf(contactName) >= 0 ? true : false;
                    });
                  };
                  

                  獲取第一個聯系人的姓名:

                  Get the name of the first contact:

                  var getFirstContact = function() {
                    return getContacts().then(function(contacts) {
                      if ( contacts.length > 0 ) {
                        return contacts[0];
                      }
                    });
                  };
                  

                  下面是一些使用這三個函數的示例代碼:

                  And here is some example code to use these three functions:

                  // Show all contacts
                  dumpContacts();
                  
                  // Does contact 'Jane' exist?
                  contactExists("Jane").then(function(exists){
                    log("Contact 'Jane' exist: " + exists);
                  });
                  
                  getFirstContact().then(function(firstContact){
                    log("first contact: " + firstContact);
                  });
                  

                  上述例程使用全局 log() 函數.可以使用 console.log() 代替.上面的 log() 函數記錄到瀏覽器窗口,實現如下:

                  The above routines make use of a global log() function. console.log() could be used instead. The above log() function log's to the browser window and is implemented as follows:

                  function log() {
                    var args = Array.prototype.slice.call(arguments).join(", ");
                    console.log(args);
                    var output = document.getElementById('output');
                    output.innerHTML += args + "<br/>";
                  }
                  

                  并且在 html 中需要以下內容:

                  and requires the following in the html:

                  <div id='output'><br/></div>
                  

                  當上面的代碼運行時,你會看到:

                  When the above code is run, you will see:

                  Calling back-end to get contact list.
                  

                  New data received from back-end.
                  

                  三遍,沒必要.

                  如何解決這個問題?

                  此示例在 Plunker 上可以執行:http://plnkr.co/edit/6ysbNTf1lSf5b7L3sJxQ?p=preview

                  This sample is on Plunker can be executed: http://plnkr.co/edit/6ysbNTf1lSf5b7L3sJxQ?p=preview

                  推薦答案

                  只需在調用的函數中緩存結果即可:

                  Just cache the result in the function making the call:

                  function cache(promiseReturningFn){
                      var cachedVal = null;  // start without cached value
                      function cached(){
                          if(cachedVal) return cachedVal; // prefer cached result
                          cachedVal = promiseReturningFn.apply(this, arguments); // delegate
                          return cachedVal; // after we saved it, return it
                      }
                      cached.flush = function(){ cachedVal = undefined; };
                      return cached;
                  }
                  

                  這有一個警告,即對于 null 的實際結果會失敗,否則它可以很好地完成工作.

                  This has the caveat of failing for actual results that are null but otherwise it gets the job done nicely.

                  您現在可以緩存任何 promise 返回函數 - 上面的版本只緩存忽略參數 - 但您可以構造一個類似的函數,它具有 Map 并基于不同的參數進行緩存 - 但讓我們專注于您的用例.

                  You can now cache any promise returning function - the version above only caches ignoring arguments - but you can construct a similar one that has a Map and caches based on different arguments too - but let's focus on your use case.

                  var getContactsCached = cache(getContacts);
                  
                  getContactsCached();
                  getContactsCached();
                  getContactsCached(); // only one async call ever made
                  

                  cache 方法實際上甚至與 Promise 無關——它所做的只是獲取一個函數并緩存它的結果——你可以將它用于任何事情.事實上,如果你正在使用像 underscore 這樣的庫,你可以使用 _.memoize 來為你做這件事.

                  The cache method is actually not even related to promises - all it does is take a function and cache its result - you can use it for anything. In fact if you're using a library like underscore you can use _.memoize to do it for you already.

                  這篇關于如何處理對后端服務進行相同調用的多個瀏覽器腳本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調用完成)
                  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 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標頭) - IT屋-程序員軟件開發技術分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                  NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 異常 101)
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內容)

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

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

                        <bdo id='CjdDH'></bdo><ul id='CjdDH'></ul>
                          <tbody id='CjdDH'></tbody>
                      • <tfoot id='CjdDH'></tfoot>

                            主站蜘蛛池模板: 亚洲精品久久久久中文字幕欢迎你 | 成人av影院| a级大毛片| 国产一级免费视频 | 欧美精品一区二区三区在线 | 国产成人精品一区二区三区在线 | 午夜免费电影院 | 暖暖成人免费视频 | 欧美精品欧美精品系列 | 国产在线一区二区三区 | 亚洲人成人一区二区在线观看 | 自拍 亚洲 欧美 老师 丝袜 | 无码国模国产在线观看 | 中文字幕 国产 | 国产日韩一区二区三区 | 91在线免费视频 | 91操操操 | 九九久久久久久 | 日本又色又爽又黄又高潮 | av日韩高清 | 亚洲成人av | 国产在线永久免费 | 国产在线精品一区二区三区 | 夜夜草| 天天搞天天搞 | 亚洲午夜av久久乱码 | 国产精品一区二区久久 | 欧美极品在线观看 | 欧美日韩在线一区二区 | 欧美日韩亚洲国产综合 | 久久久99精品免费观看 | 久久丝袜| 一区二区久久电影 | 婷婷综合色 | 一级一片在线观看 | 五月婷婷 六月丁香 | 亚洲免费在线播放 | 精品综合久久久 | 久久精品亚洲一区 | 亚洲国产精品成人无久久精品 | 欧美一区二区在线 |