pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<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="vxrtlpj" 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="tvfxpxl" 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="xz1ldbb" 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>

                            主站蜘蛛池模板: 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 液压油缸-液压站生产厂家-洛阳泰诺液压科技有限公司 | 电动百叶窗,开窗器,电动遮阳百叶,电动开窗机生产厂家-徐州鑫友工控科技发展有限公司 | 缠膜机|缠绕包装机|无纺布包装机-济南达伦特机械设备有限公司 | 首页-瓜尔胶系列-化工单体系列-油田压裂助剂-瓜尔胶厂家-山东广浦生物科技有限公司 | 制冷采购电子商务平台——制冷大市场 | 威实软件_软件定制开发_OA_OA办公系统_OA系统_办公自动化软件 | 首页|专注深圳注册公司,代理记账报税,注册商标代理,工商变更,企业400电话等企业一站式服务-慧用心 | 柔软云母板-硬质-水位计云母片组件-首页-武汉长丰云母绝缘材料有限公司 | 无线遥控更衣吊篮_IC卡更衣吊篮_电动更衣吊篮配件_煤矿更衣吊篮-力得电子 | 冷水机,风冷冷水机,水冷冷水机,螺杆冷水机专业制造商-上海祝松机械有限公司 | 【星耀裂变】_企微SCRM_任务宝_视频号分销裂变_企业微信裂变增长_私域流量_裂变营销 | nalgene洗瓶,nalgene量筒,nalgene窄口瓶,nalgene放水口大瓶,浙江省nalgene代理-杭州雷琪实验器材有限公司 | 北京翻译公司_同传翻译_字幕翻译_合同翻译_英语陪同翻译_影视翻译_翻译盖章-译铭信息 | 酒精检测棒,数显温湿度计,酒安酒精测试仪,酒精检测仪,呼气式酒精检测仪-郑州欧诺仪器有限公司 | 酒瓶_酒杯_玻璃瓶生产厂家_徐州明政玻璃制品有限公司 | 挨踢网-大家的导航! | 镀锌钢格栅_热镀锌格栅板_钢格栅板_热镀锌钢格板-安平县昊泽丝网制品有限公司 | 胶泥瓷砖胶,轻质粉刷石膏,嵌缝石膏厂家,腻子粉批发,永康家德兴,永康市家德兴建材厂 | 金蝶帐无忧|云代账软件|智能财税软件|会计代账公司专用软件 | 企业管理培训,企业培训公开课,企业内训课程,企业培训师 - 名课堂企业管理培训网 | 南京欧陆电气股份有限公司-风力发电机官网| 真石漆,山东真石漆,真石漆厂家,真石漆价格-山东新佳涂料有限公司 | 武汉刮刮奖_刮刮卡印刷厂_为企业提供门票印刷_武汉合格证印刷_现金劵代金券印刷制作 - 武汉泽雅印刷有限公司 | 智能型高压核相仪-自动开口闪点测试仪-QJ41A电雷管测试仪|上海妙定 | 陕西安闸机-伸缩门-车牌识别-广告道闸——捷申达门业科技 | RV减速机-蜗轮蜗杆减速机-洗车机减速机-减速机厂家-艾思捷 | 锂电池砂磨机|石墨烯砂磨机|碳纳米管砂磨机-常州市奥能达机械设备有限公司 | 应急灯_消防应急灯_应急照明灯_应急灯厂家-大成智慧官网 | 液压升降平台_剪叉式液压/导轨式升降机_传菜机定做「宁波日腾升降机厂家」 | 股指期货-期货开户-交易手续费佣金加1分-保证金低-期货公司排名靠前-万利信息开户 | 净化车间装修_合肥厂房无尘室设计_合肥工厂洁净工程装修公司-安徽盛世和居装饰 | LED投光灯-工矿灯-led路灯头-工业灯具 - 山东普瑞斯照明科技有限公司 | 渣油泵,KCB齿轮泵,不锈钢齿轮泵,重油泵,煤焦油泵,泊头市泰邦泵阀制造有限公司 | 防爆型气象站_农业气象站_校园气象站_农业四情监测系统「山东万象环境科技有限公司」 | 扫地车厂家-山西洗地机-太原电动扫地车「大同朔州吕梁晋中忻州长治晋城洗地机」山西锦力环保科技有限公司 | 膜结构_ETFE膜结构_膜结构厂家_膜结构设计-深圳市烨兴智能空间技术有限公司 | 温州富欧金属封头-不锈钢封头厂家 | 学生作文网_中小学生作文大全与写作指导| 不发火防静电金属骨料_无机磨石_水泥自流平_修补砂浆厂家「圣威特」 |