开发者

How do I use jQuery .when() function with a dynamic set of ajax calls?

开发者 https://www.devze.com 2023-03-19 17:29 出处:网络
I\'m using JqGrid and for each row in the grid I\'m loading, I am making an ajax call to get additional data.

I'm using JqGrid and for each row in the grid I'm loading, I am making an ajax call to get additional data.

Once that's all complete, I need to apply some formatting.

I would like to use $.when(), but I'm not sure how to call it. I was researching the apply() method, but I still don't see how to use it appropriately.

Here is my code:

 $(rows).each(function () {
                        $.ajax(
                        {
                            url: url,
                            data: data,
                            success: function (result) {

                                }
                   开发者_运维问答         }
                        });
                    });

   $.when(**What do i pass here??**).done(function () {

                    });

I had tried pushing each $.ajax call to an array, but I can't pass the array directly, and call everything.

Thanks in advance for your help!


This may not work at all, in fact I'm curious as to whether or not it will. Try building the array of promise objects, then call $.when.apply(null, arr).done(function () { ... });

apply allows you to trigger a function and pass an array of arguments dynamically, such as in this case.

0

精彩评论

暂无评论...
验证码 换一张
取 消