简体   繁体   English

循环中有多个AJAX调用

[英]Multiple AJAX calls in loop

I need a way to send multiple AJAX calls at the same time in Javascript/Angular. 我需要一种方法在Javascript / Angular中同时发送多个AJAX调用。 After some searching i couldn't find an answer. 经过一番搜索,我找不到答案。 What i want to do is send all my requests as fast as possible. 我想要做的是尽快发送我的所有请求。 If i execute my calls in a for loop or in a queue of promises with the $q library in Angular, a request gets sent, waits for it to execute the callback, and then sends the next one. 如果我使用Angular中的$ q库在for循环或promises队列中执行我的调用,则会发送一个请求,等待它执行回调,然后发送下一个。 This is a example code: 这是一个示例代码:

var array = [];
    Page.get({id:1}, function(result){
        for(var i = 0; i < result.region[0].hotspots.length; i++){
            var promise = Hotspot.get({id: result.region[0].hotspots[i].id});
            array.push(promise);
        }
        $q.all(array).then(function(data){
            console.log(data);
        });
    });

Page is a angular resource with a get method which requires a ID. Page是一个带有get方法的角度资源,需要ID。

What i want is that they all get sent at the same time and they call their callback when ready. 我想要的是他们都在同一时间被发送,他们准备好后回拨他们的回叫。 The order in which the calls get returned doesn't really matter. 返回调用的顺序并不重要。

Thanks 谢谢

Think outside the box with Web Workers 与Web Workers一起思考

An interesting aproach to solve this question, is to use web workers to execute the requests in a different thread . 解决这个问题的一个有趣的方法是使用Web worker不同的线程中执行请求。 if you are not familiar with web workers i advice you to start by this great tutorial of techsith . 如果你不熟悉网络工作者,我建议你从这个伟大的技术教程 开始 Basically, you will be able to execute multiple jobs at the same time. 基本上,您将能够同时执行多个作业。 See also the W3Schools Documentation . 另请参阅W3Schools文档

在此输入图像描述

This article from Html5Rocks teach us how to use Web Workers without a separate script file. 来自Html5Rocks的这篇文章教我们如何在没有单独的脚本文件的情况下使用Web Workers。

Have you tried using Async.js module? 您是否尝试过使用Async.js模块?

You can achieve desired behavior using something like 您可以使用类似的方式实现所需的行为

Page.get({id:1}, function(result){
    async.each(result.region[0].hotspots, callAsync, function(err, res){
        console.log(res);
    }
});

function callAsync(hotspot, callback){
    callback(null, Hotspot.get({id: hotspot.id});
}

From Doc : 来自Doc:

each(coll, iteratee, [callback]) 每个(coll,iteratee,[callback])

Applies the function iteratee to each item in coll, in parallel. 将函数iteratee并行应用于coll中的每个项目。 The iteratee is called with an item from the list, and a callback for when it has finished. 使用列表中的项目调用iteratee,并在完成时调用它。 If the iteratee passes an error to its callback, the main callback (for the each function) is immediately called with the error. 如果iteratee将错误传递给其回调,则会立即调用主回调(对于每个函数)并显示错误。

The $http service sends XHRs in parallel. $http服务并行发送XHR。 The code below demostrates 10 XHRs being sent to httpbin.org and subsequently being received in a different order. 下面的代码演示了10个XHR被发送到httpbin.org并随后以不同的顺序接收。

angular.module('myApp').controller('myVm', function ($scope, $http) {
    var vm = $scope;
    vm.sentList = [];
    vm.rcvList = [];
    //XHR with delay from 9 to 1 seconds
    for (var n=9; n>0; n--) {
        var url = "https://httpbin.org/delay/" + n;
        vm.sentList.push(url);
        $http.get(url).then(function (response) {
             vm.rcvList.push(response.data.url);
        });
    };
    //XHR with 3 second delay
    var url = "https://httpbin.org/delay/3";
    vm.sentList.push(url);
    $http.get(url).then(function (response) {
         vm.rcvList.push(response.data.url);
    })
})

The DEMO on JSFiddle . JSFiddle上DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM