如何使用给定数量的流执行顺序异步Ajax请求 [英] How to do sequential asynchronous ajax requests with given number of streams

查看:98
本文介绍了如何使用给定数量的流执行顺序异步Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用有限的流来创建顺序异步ajax请求。截至目前,我被允许在Web服务器上只占用一个流,所以我只能在时间上执行一个ajax请求。

I need to make sequential asynchronous ajax requests with limited streams. As of now I am allowed to occupy only one stream on web server so I can only do one ajax request at time.

我有以下功能可以帮助我当我允许一次只使用一个流。

I have following function which helps me when I am allowed to use only one stream at a time.

function initiateChain() {
    var i = 0;
    var tasks = arguments;
    var callback = function () {
      i += 1;
      if (i != tasks.length) {
        tasks[i](callback); //block should call callback when done otherwise loop stops
      }
    }
    if (tasks.length != 0) {
      tasks[0](callback); //initiate first one
    }
  }

说我是否有三个ajax辅助函数

Say if I have three ajax helper functions

 function getGadgets(callback) {
         //ajax call
         callback(); // I call this in complete callback of $.ajax
 }

 function getBooks(callback) {
         //ajax call
         callback(); // I call this in complete callback of $.ajax
 }

 function getDeals(callback) {
         //ajax call
         callback(); // I call this in complete callback of $.ajax
 }

以下呼叫确保从这个客户端发出的请求不超过1个

following call ensures that no more than 1 ajax request is made from this client

initiateChain(getGadgets, getBooks, getDeals);

现在我需要增强initiateChain以支持任意数量的流。假设我可以使用2或n个数据流,我想知道这样做,而无需更改ajax辅助函数getGadgets,getDeals,getDeals。

Now I need to enhance initiateChain to support arbitrary number of streams. Say I am allowed to use 2 or n number of streams I would like to know ideas to do so without changing ajax helper functions getGadgets, getDeals, getDeals.

简而言之,我有一组函数N,在这种情况下,getGadgets,getDeals和getDeals(| N | = 3)每个都需要连接到Web服务器。目前,我一次只能执行一个请求,因此initiateChain函数按顺序调用这三个方法。如果我有权访问M连接,我想执行| N |函数并行(最多M个)。

In short, I have a set of functions, N, in this case getGadgets, getDeals and getDeals(|N|=3) that each need a connection to the web server. Currently, I can only execute one request at a time, so initiateChain function calls the three methods in sequence. If I had access to M connections, I would like to execute |N| functions in parallel (up to a maximum of M).

推荐答案

如果您正在使用jQuery,那么您可以使用它的 .queue 方法排队ajax调用,然后按顺序执行它们。要运行多个序列,可以将初始出列包装在循环中。

If you're using jQuery then you can use its .queue method to queue up your ajax calls and then execute them in sequence. To run multiple sequences, you can wrap the initial dequeue in a loop.

function add_api_call_to_queue(qname, api_url) {
    $(document).queue(qname, function() {
        $.ajax({
            type     : 'GET',
            async    : true,
            url      : api_url,
            dataType : 'json',
            success  : function(data, textStatus, jqXHR) {
                // activate the next ajax call when this one finishes
                $(document).dequeue(qname);
            }
        });
    });
}

$(document).ready(function() {

    var queue_name       = 'a_queue';
    var concurrent_calls = 2;

    // add first AJAX call to queue
    add_api_call_to_queue(queue_name, '/example/api/books');

    // add second AJAX call to queue
    add_api_call_to_queue(queue_name, '/example/api/dvds');

    // add third AJAX call to queue
    add_api_call_to_queue(queue_name, '/example/api/shoes');

    // start the AJAX queue
    for (i=0;i<concurrent_calls;i++) {
        $(document).dequeue(queue_name);
    }

})

这篇关于如何使用给定数量的流执行顺序异步Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆