jQuery Ajax:如何等待* async *请求成功完成后再继续?

时间:2022-08-23 18:00:24

I'm having a problem making ajax fast and functional. Here's the pseudo/prototype code:

我在制作ajax快速且功能性方面遇到了问题。这是伪/原型代码:

function blah1(arg1){//arg1 is an array, roughly 10 elements

var arr[];

$.each(arg1, function(i){
//blah code

    $.ajax({
        //blah options
        async:   true,
        success: function(data){
            arr[i] = data.someInt;
        }//end success
    });//end ajax
}//end each

return arr;
}//end function

Basically, I'm sending an ajax and need the returned data for further processing.

基本上,我发送一个ajax并需要返回的数据进行进一步处理。

If I set async to true, the function immediately returns empty 'arr' array, thus the whole script fails. But if I set async to false, then it works, but takes very long.

如果我将async设置为true,则函数会立即返回空的'arr'数组,因此整个脚本都会失败。但是,如果我将异步设置为false,那么它可以工作,但需要很长时间。

I have seen this $.ajaxQueue(); thing, but frankly I don't understand it at all, and I don't know if it will work.

我见过这个$ .ajaxQueue();事情,但坦率地说,我根本不理解它,我不知道它是否会奏效。

So the question is, firstly, is there any way I can asynchronously send all the ajax requests at the same time and let function wait and return arr[] after all ajax are done? If not, will the ajaxQueue work in my case? (rough example please?)

所以问题是,首先,有没有什么方法可以同时异步发送所有的ajax请求,让函数等待并在所有ajax完成后返回arr []?如果没有,ajaxQueue会在我的情况下工作吗? (粗略的例子,请?)

2 个解决方案

#1


5  

Using jQuery 1.5 deferred's I would opt for this :

使用jQuery 1.5 deferred's我会选择这个:

function blah1(arr, callback){
    $.when($.map(arr, function(val, i){
        $.ajax({
            //blah options
            async:   true
        });
    }).toArray()).then(function(resultsArr) {
         callback(resultsArr);
    });
}

The problem was you were trying to return the array in your function before the async ajax calls finish. This isn't really possible so you will need to pass a callback to blah.

问题是你试图在异步ajax调用完成之前返回函数中的数组。这实际上是不可能的,所以你需要将回调传递给blah。

What your doing here is mapping your array of objects to jqXHR objects (which are deferred objects). Then passing that array of deferred objects to $.when.

你在这里做的是将你的对象数组映射到jqXHR对象(它们是延迟对象)。然后将该延迟对象数组传递给$ .when。

$.when takes an array and then allows you to run the .then function when the entire array has finished loading from the ajax calls. You then have a resultsArr passed in as an argument to your .then function.

$ .when接受一个数组然后允许你在整个数组从ajax调用完成加载时运行.then函数。然后,您将一个resultsArr作为参数传递给.then函数。

There is no way to use $.ajax and return in the same function if you manipulate the return value in your ajax success call.

如果您在ajax成功调用中操作返回值,则无法使用$ .ajax并返回相同的函数。

#2


4  

You could make the Ajax call synchronous which you seem to know about, personally I would re factor my code so the success method of the ajax call, then triggers a call off to another function.

您可以使Ajax调用同步,您似乎知道,个人我会重新考虑我的代码,因此ajax调用的成功方法,然后触发另一个函数的调用。

$.ajax({
        //blah options
        async:   true,
        success: function(data){
            arr[i] = data.someInt;
            myCall(arr[i]);
        }//end success
    });//end ajax

#1


5  

Using jQuery 1.5 deferred's I would opt for this :

使用jQuery 1.5 deferred's我会选择这个:

function blah1(arr, callback){
    $.when($.map(arr, function(val, i){
        $.ajax({
            //blah options
            async:   true
        });
    }).toArray()).then(function(resultsArr) {
         callback(resultsArr);
    });
}

The problem was you were trying to return the array in your function before the async ajax calls finish. This isn't really possible so you will need to pass a callback to blah.

问题是你试图在异步ajax调用完成之前返回函数中的数组。这实际上是不可能的,所以你需要将回调传递给blah。

What your doing here is mapping your array of objects to jqXHR objects (which are deferred objects). Then passing that array of deferred objects to $.when.

你在这里做的是将你的对象数组映射到jqXHR对象(它们是延迟对象)。然后将该延迟对象数组传递给$ .when。

$.when takes an array and then allows you to run the .then function when the entire array has finished loading from the ajax calls. You then have a resultsArr passed in as an argument to your .then function.

$ .when接受一个数组然后允许你在整个数组从ajax调用完成加载时运行.then函数。然后,您将一个resultsArr作为参数传递给.then函数。

There is no way to use $.ajax and return in the same function if you manipulate the return value in your ajax success call.

如果您在ajax成功调用中操作返回值,则无法使用$ .ajax并返回相同的函数。

#2


4  

You could make the Ajax call synchronous which you seem to know about, personally I would re factor my code so the success method of the ajax call, then triggers a call off to another function.

您可以使Ajax调用同步,您似乎知道,个人我会重新考虑我的代码,因此ajax调用的成功方法,然后触发另一个函数的调用。

$.ajax({
        //blah options
        async:   true,
        success: function(data){
            arr[i] = data.someInt;
            myCall(arr[i]);
        }//end success
    });//end ajax