使用可变数量的参数进行故障排除时的jQuery. [英] jQuery .when troubleshooting with variable number of arguments

查看:39
本文介绍了使用可变数量的参数进行故障排除时的jQuery.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用jQuery.when()等待多个ajax请求完成之前,我遇到了一个问题,然后调用了另一个函数.

I'm having an issue with using jQuery.when() to wait for multiple ajax requests to finish before calling another function.

每个ajax请求都将获取JSON数据,如下所示:

Each ajax request will get JSON data, and looks something like this:

   function loadData(arg){
        var ajaxCall = $.ajax(
            URL // depends on arg
        )
       .error( .... );
       return ajaxCall;
   }

调用请求时,会将返​​回值(ajaxCall)添加到名为ajaxRequests的列表中.

When the request is called, the return value (ajaxCall) is added to a list called ajaxRequests.

    ajaxRequests = [];
    ajaxREquests.push(loadData(arg))

所有请求都发出后,我正试图将ajaxRequests传递给$ .when,以等待所有请求完成.

When all the requests have been made, I'm trying to pass ajaxRequests to $.when in order to wait for all requests to complete.

        var defer = $.when.apply($, ajaxRequests);
        defer.done(function(args){
            for (var i=0; i<args.length; i++){
                inst.loadData($.parseJSON(args[i].responseText));
            }
            inst.draw();
        });

inst是一个基于JSON数据加载和绘制图形的对象.

inst is an object that loads and draws graphs based on JSON data.

问题在于它似乎并没有真正在等待请求完成-args [i]是一个对象,但是在代码运行时responseText是未定义的.如果我保存args [i]并稍后从控制台访问它,那么它将起作用.

The problem is that it doesn't seem to be actually waiting for the requests to finish - args[i] is an object, but responseText is undefined when the code runs. If I save args[i] and access it later from the console, it works.

我怀疑问题与使用.when带有任意数量的参数有关,因为我在网络上看到的所有示例都为它提供了预定义的参数列表.

I suspect the problem is related to using .when with an arbitrary number of arguments, as all the examples I've seen on the web give it a pre-defined argument list.

我不确定使用apply是否是正确的主意,但是无论哪种方式,它都无法正常工作并且行为不正常(取决于浏览器).

I'm not sure if using apply was the right idea or not, but either way it doesn't work properly and behaves erratically (browser-dependent).

任何帮助将不胜感激.

如果需要更多信息,请告诉我.
我正在使用jQuery 1.5

Please let me know if more information is required.
I'm using jQuery 1.5

推荐答案

尽管Alex确实提供了解决他的问题的方法,但我发现遵循它有些困难.我遇到了一个与他解决的问题类似的问题,我想与其他需要处理各种数量的ajax请求的人分享我的解决方案.

Although Alex did indeed provide a solution to his problem, I found following it a bit difficult. I had an issue similar to his that I solved, and I wanted to share my solution for anyone else who needs to process a variable number of ajax requests.

// Array of requests
var requests = Array();
requests.push($.get('responsePage.php?data=foo'));
requests.push($.get('responsePage.php?data=bar'));

var defer = $.when.apply($, requests);
defer.done(function(){

    // This is executed only after every ajax request has been completed

    $.each(arguments, function(index, responseData){
        // "responseData" will contain an array of response information for each specific request
    });

});

这篇关于使用可变数量的参数进行故障排除时的jQuery.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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