<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 请求进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc
                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                  • <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>