weixin_33728708 2017-06-22 18:47 采纳率: 0%
浏览 47

$ .whenall与Ajax

I have a function for returning a feed which is retrieved by an AJAX-Call and now want to do something after a few of these asynchron requests have been done.

doit() is the function I call at first.

I am sorry for not providing a url, but it is an internal server.

Here is my code:

   function grabFollowedCommunityPageFeed(page, cCallback) {
                $.ajax({
                    url: "blabla.com&page=" + page,
                    method: "GET",
                    contentType: "application/atom+xml"
                }).always(function(xhr, ignore, thrownMessage) {
                    var totalResults = 0;
                    if ((200 === thrownMessage.status) && (xhr)) {
                        totalResults = parseInt($(xhr).find("totalResults").first().text()) || -1;
                    }
                    if (cCallback && $.isFunction(cCallback)) {
                        cCallback({feed: xhr, resultCount: totalResults});
                    }
                });
            }

            function grabFollowedCommunitiesFeeds(pagecount) {
                var i = 1,
                    deferredArr = [];
                for (i = 1; i < pagecount; i += 1) {
                    grabFollowedCommunityPageFeed(i, function callback(resultObj) {
                        deferredArr[i] = new $.Deferred();
                        deferredArr[i].resolve(resultObj);
                    });
                }
                return deferredArr;
            }


            function doit() {
                var allCommunityFeedObjects = [],
                    allCommunityFeedObjectsCount = 0,
                    deferredObj = [];
                (function initialReadFollowedCommunityFeedPages() {
                    grabFollowedCommunityPageFeed(1, function(requestObj) {
                        allCommunityFeedObjectsCount = requestObj.resultCount;
                        var tEntries = $(requestObj.communityFeed).find("entry"),
                            el$;
                        $.each(tEntries, function(ignore, el) {
                            el$ = $(el);
                            if (!($.inArray(el$, allCommunityFeedObjects) !== -1)) {
                                allCommunityFeedObjects.push(el$);
                            }
                        });

                        deferredObj = grabFollowedCommunitiesFeeds(allCommunityFeedObjectsCount) || [];
                        $.whenAll.apply($, deferredObj).always(function(allCommunityFeeds) {
                            var k = allCommunityFeeds;
                            // union k with allCommunityFeedObjects
                        });
                    });
                })();

            }

This line seems to be fine as well and I have checked it:

deferredArr[i].resolve(resultObj);

The problem is that allCommunityFeeds parameter is undefined in

  $.whenAll.apply($, deferredObj).always(function(allCommunityFeeds) 

and that means there is something wrong. Can you help me?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 求数学坐标画圆以及直线的算法
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 自己瞎改改,结果现在又运行不了了
    • ¥15 链式存储应该如何解决
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站