jquery parallel ajax一个接一个地调用循环

时间:2021-08-29 20:26:37

When i execute below script it creating all 1000 ajax requests and when the transaction gets to about ~3k it never did finish. so i am trying to make slab based parallel ajax requests,

当我执行下面的脚本时,它创建所有1000个ajax请求,当事务达到约3k时,它从未完成。所以我试图制作基于平板的并行ajax请求,

How do i create parallel ajax requests one after another? I want to create first 100 parallal ajax requests after completing the 1st 100 ajax responses then create 2nd position 200 parallel ajax requests.

我如何一个接一个地创建并行ajax请求?我想在完成前100个ajax响应之后创建前100个parallal ajax请求,然后创建第二个位置200个并行ajax请求。

Here is my array

这是我的阵列

var tasks=[{100 urls},{200 urls},{400 urls},{600 urls},{800 urls},{1000 urls}];
$.each(tasks,function(index,item){
  $.each(item.urls,function(key,val){
    $.aja(val.url,function(response){
    // .... do some work
    });
  });
});

1 个解决方案

#1


-1  

Every ajax call runs asynchronously. So you may wait each task's async ajax calls done and run next task and so on.

每个ajax调用都是异步运行的。因此,您可以等待每个任务的异步ajax调用完成并运行下一个任务,依此类推。

Below script uses Promise pattern(jquery.when) to wait done each task's async ajax calls.

下面的脚本使用Promise模式(jquery.when)来等待每个任务的异步ajax调用。

var tasks=[{100 urls},{200 urls},{400 urls},{600 urls},{800 urls},{1000 urls}];

function runTasks(index) {
  var item = tasks[index];
  return $.when($.map(item.urls, function(key, val) {
    return $.ajax(val.url);
  })).then(function() {
    if (tasks[index + 1]) {
      return runTask(index + 1);
    } else {
      return true;
    }
  });
}

runTasks(0).then(function() {
  // execute when all tasks done
});

#1


-1  

Every ajax call runs asynchronously. So you may wait each task's async ajax calls done and run next task and so on.

每个ajax调用都是异步运行的。因此,您可以等待每个任务的异步ajax调用完成并运行下一个任务,依此类推。

Below script uses Promise pattern(jquery.when) to wait done each task's async ajax calls.

下面的脚本使用Promise模式(jquery.when)来等待每个任务的异步ajax调用。

var tasks=[{100 urls},{200 urls},{400 urls},{600 urls},{800 urls},{1000 urls}];

function runTasks(index) {
  var item = tasks[index];
  return $.when($.map(item.urls, function(key, val) {
    return $.ajax(val.url);
  })).then(function() {
    if (tasks[index + 1]) {
      return runTask(index + 1);
    } else {
      return true;
    }
  });
}

runTasks(0).then(function() {
  // execute when all tasks done
});