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

  • <tfoot id='LOn5X'></tfoot>
  • <legend id='LOn5X'><style id='LOn5X'><dir id='LOn5X'><q id='LOn5X'></q></dir></style></legend>
    • <bdo id='LOn5X'></bdo><ul id='LOn5X'></ul>

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

      1. 对 ajax 请求进行排序

        Sequencing ajax requests(对 ajax 请求进行排序)
          <tbody id='InCXM'></tbody>
        <tfoot id='InCXM'></tfoot>
      2. <legend id='InCXM'><style id='InCXM'><dir id='InCXM'><q id='InCXM'></q></dir></style></legend>
          <bdo id='InCXM'></bdo><ul id='InCXM'></ul>

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

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

                  本文介绍了对 ajax 请求进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我发现有时我需要迭代一些集合并对每个元素进行 ajax 调用.我希望每个调用在移动到下一个元素之前返回,这样我就不会向服务器发送请求——这通常会导致其他问题.而且我不想将 async 设置为 false 并冻结浏览器.

                  I find I sometimes need to iterate some collection and make an ajax call for each element. I want each call to return before moving to the next element so that I don't blast the server with requests - which often leads to other issues. And I don't want to set async to false and freeze the browser.

                  通常这涉及设置某种迭代器上下文,我会在每次成功回调时逐步执行该上下文.我认为必须有一个更清洁更简单的方法?

                  Usually this involves setting up some kind of iterator context that i step thru upon each success callback. I think there must be a cleaner simpler way?

                  有没有人有一个聪明的设计模式,如何通过一个集合来巧妙地工作,为每个项目进行 ajax 调用?

                  Does anyone have a clever design pattern for how to neatly work thru a collection making ajax calls for each item?

                  推荐答案

                  jQuery 1.5+

                  我开发了一个$.ajaxQueue() 使用 $.Deferred 的插件,.queue()$.ajax() 也传回 promise 在请求完成时解析.

                  /*
                  * jQuery.ajaxQueue - A queue for ajax requests
                  * 
                  * (c) 2011 Corey Frang
                  * Dual licensed under the MIT and GPL licenses.
                  *
                  * Requires jQuery 1.5+
                  */ 
                  (function($) {
                  
                  // jQuery on an empty object, we are going to use this as our Queue
                  var ajaxQueue = $({});
                  
                  $.ajaxQueue = function( ajaxOpts ) {
                      var jqXHR,
                          dfd = $.Deferred(),
                          promise = dfd.promise();
                  
                      // queue our ajax request
                      ajaxQueue.queue( doRequest );
                  
                      // add the abort method
                      promise.abort = function( statusText ) {
                  
                          // proxy abort to the jqXHR if it is active
                          if ( jqXHR ) {
                              return jqXHR.abort( statusText );
                          }
                  
                          // if there wasn't already a jqXHR we need to remove from queue
                          var queue = ajaxQueue.queue(),
                              index = $.inArray( doRequest, queue );
                  
                          if ( index > -1 ) {
                              queue.splice( index, 1 );
                          }
                  
                          // and then reject the deferred
                          dfd.rejectWith( ajaxOpts.context || ajaxOpts,
                              [ promise, statusText, "" ] );
                  
                          return promise;
                      };
                  
                      // run the actual query
                      function doRequest( next ) {
                          jqXHR = $.ajax( ajaxOpts )
                              .done( dfd.resolve )
                              .fail( dfd.reject )
                              .then( next, next );
                      }
                  
                      return promise;
                  };
                  
                  })(jQuery);
                  

                  jQuery 1.4

                  如果您使用的是 jQuery 1.4,您可以利用空对象上的动画队列来为您对元素的 ajax 请求创建自己的队列".

                  jQuery 1.4

                  If you're using jQuery 1.4, you can utilize the animation queue on an empty object to create your own "queue" for your ajax requests for the elements.

                  您甚至可以将其纳入您自己的 $.ajax() 替换中.此插件 $.ajaxQueue() 使用 jQuery 的标准 'fx' 队列,如果队列尚未运行,它将自动启动第一个添加的元素.

                  You can even factor this into your own $.ajax() replacement. This plugin $.ajaxQueue() uses the standard 'fx' queue for jQuery, which will auto-start the first added element if the queue isn't already running.

                  (function($) {
                    // jQuery on an empty object, we are going to use this as our Queue
                    var ajaxQueue = $({});
                  
                    $.ajaxQueue = function(ajaxOpts) {
                      // hold the original complete function
                      var oldComplete = ajaxOpts.complete;
                  
                      // queue our ajax request
                      ajaxQueue.queue(function(next) {
                  
                        // create a complete callback to fire the next event in the queue
                        ajaxOpts.complete = function() {
                          // fire the original complete if it was there
                          if (oldComplete) oldComplete.apply(this, arguments);
                  
                          next(); // run the next query in the queue
                        };
                  
                        // run the query
                        $.ajax(ajaxOpts);
                      });
                    };
                  
                  })(jQuery);
                  

                  示例用法

                  所以,我们有一个 <ul id="items"> 有一些 <li> 我们想要复制(使用 ajax!)到<ul id="output">

                  Example Usage

                  So, we have a <ul id="items"> which has some <li> that we want to copy (using ajax!) to the <ul id="output">

                  // get each item we want to copy
                  $("#items li").each(function(idx) {
                  
                      // queue up an ajax request
                      $.ajaxQueue({
                          url: '/echo/html/',
                          data: {html : "["+idx+"] "+$(this).html()},
                          type: 'POST',
                          success: function(data) {
                              // Write to #output
                              $("#output").append($("<li>", { html: data }));
                          }
                      });
                  });
                  

                  jsfiddle 演示 - 1.4版本

                  这篇关于对 ajax 请求进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                  • <bdo id='1bEA7'></bdo><ul id='1bEA7'></ul>
                    <i id='1bEA7'><tr id='1bEA7'><dt id='1bEA7'><q id='1bEA7'><span id='1bEA7'><b id='1bEA7'><form id='1bEA7'><ins id='1bEA7'></ins><ul id='1bEA7'></ul><sub id='1bEA7'></sub></form><legend id='1bEA7'></legend><bdo id='1bEA7'><pre id='1bEA7'><center id='1bEA7'></center></pre></bdo></b><th id='1bEA7'></th></span></q></dt></tr></i><div id='1bEA7'><tfoot id='1bEA7'></tfoot><dl id='1bEA7'><fieldset id='1bEA7'></fieldset></dl></div>

                    <small id='1bEA7'></small><noframes id='1bEA7'>

                          <tbody id='1bEA7'></tbody>
                        • <tfoot id='1bEA7'></tfoot>
                        • <legend id='1bEA7'><style id='1bEA7'><dir id='1bEA7'><q id='1bEA7'></q></dir></style></legend>