如何自动链接一系列ajax请求?

时间:2012-03-11 21:25:27

标签: javascript jquery

看看我的功能的下半部分:我想重复几次info(url_part1 + next + url_part2, function(next) {。有没有比下面介绍的更聪明的方式(可能是某种循环)?我一直在思考,我无法设计任何东西。

 function info(link, callback) {
        $.getJSON(link, function(json) {
            $.each(json.data.children, function(i, things) {
                $("#threadlist").append('<img src="' + things.data.url + '">');
            });
            callback(json.data.after);
        });
    }
    var url_first = "http://www.reddit.com/r/aww/.json?jsonp=?";
    var url_part1 = "http://www.reddit.com/r/aww/.json?after=";
    var url_part2 = "&jsonp=?";

    info(url_first, function(next) {
        info(url_part1 + next + url_part2, function(next) {
            info(url_part1 + next + url_part2, function(next) {
                info(url_part1 + next + url_part2, function(next) {
                    info(url_part1 + next + url_part2, function(next) {

                    });
                });
            });
        });
    });

Js fiddle:http://jsfiddle.net/rdUBD/1/

3 个答案:

答案 0 :(得分:2)

也许是这样的:

http://jsfiddle.net/JwAbK/

答案 1 :(得分:1)

您可能需要考虑递归处理它们。

function processRequest( request)
{
      $.getJSON(request,function(json) {
          // process json
          if (json.data.after) {
             processRequest( "http://www.reddit.com/r/aww/.json?after="
                                 + json.data.after 
                                 + "&jsonp=?" );
          }
      });
}

processRequest("http://www.reddit.com/r/aww/.json?jsonp=?");

答案 2 :(得分:0)

如果我理解正确,似乎你要做的就是递归调用一个函数,最多可以调用一次。以下是完全相同的示例代码:

function testFunction(number, callback) {
    document.writeln("testFunction() called with number = " + number + "<br />");
    callback(number + 1);
}

var CallCount = 0;
testFunction(1, function(next) {
    CallCount++;
    document.writeln("callback called next = " + next + "<br />");

    if(CallCount == 5) {
        document.writeln("callback called 5 times, don't call it again");
    } else {
        testFunction(next, arguments.callee);
    }
});

该代码输出以下内容:

testFunction() called with number = 1
callback called next = 2
testFunction() called with number = 2
callback called next = 3
testFunction() called with number = 3
callback called next = 4
testFunction() called with number = 4
callback called next = 5
testFunction() called with number = 5
callback called next = 6
callback called 5 times, don't call it again

你可以在jFiddle上试试:http://jsfiddle.net/luisperezphd/NQya6/

将此应用于您的代码:

var DebugCount = 0;
function EmulateGetJson(link, callback) {
    DebugCount++;
    callback({
        data: {
            children: [
                { data : { url: "https://www.google.com/images/srpr/logo3w.png?" + DebugCount, next: "next" } }
            ]
        }
    });
}

 function info(link, callback) {
    //$.getJSON(link, function(json) {
    EmulateGetJson(link, function(json) {
        $.each(json.data.children, function(i, things) {
            $("#threadlist").append('<img src="' + things.data.url + '">');
        });
        callback(json.data.after);
    });
}

var url_first = "http://www.reddit.com/r/aww/.json?jsonp=?";
var url_part1 = "http://www.reddit.com/r/aww/.json?after=";
var url_part2 = "&jsonp=?";

var CallCount = 0;
var TargetCallCount = 5;
info(url_first, function(next) {
    CallCount++;

    if(CallCount != TargetCallCount) {
        info(url_part1 + next + url_part2, arguments.callee);
    }
});

可以在http://jsfiddle.net/luisperezphd/uY8Kj/

找到jsFiddle

我在JavaScript代码中模拟了对$.getJSON的调用,以提供完整的示例。我在示例图像URL的末尾添加了一个数字,以说明每次都返回不同的URL。

arguments.callee引用当前函数,在这种情况下,您作为回调函数传递的函数info()这是递归的关键。