排队ajax请求

时间:2013-08-12 18:09:06

标签: jquery ajax asynchronous

尝试建立一个“队列管理器”,但我无法完全理解如何实现这一目标。我意识到我可以简单地禁用async,但这会阻止所有事情,我需要触发一些事情。

我的想法是使用回调或任何东西来通知队列管理器您可以让另一个项目通过。我已多次尝试过,但却无法做到这一点。

这就是我的想象:

mc.queueManager = function(ajaxRequest) {
  var queue = []; // The "queue", an array

  this.request = ajaxRequest;
  queue.push(this.request);

  this.curtask = queue.shift() // Grab and remove the first item from the queue

  $.ajax({
    type: "POST",
    url: "handler.php",
    data: {"data": this.curtask},
    success: function(data) {
      // Handle data retrieved
    },
    complete: function() {
      // So, request completed, now this is the callback
      if (queue.length > 0) {
        mc.queueManager(queue.shift()); // Grab next item and repeat
      }
    }
  });
};

现在我可以理解没有什么可以防止多个请求被解雇,我现在无法弄清楚如何做到这一点。我目前正在查看文档中的$.deferred。我也尝试了使用$.when() / .then()的不同方法,但无济于事。

0 个答案:

没有答案
相关问题