简体   繁体   English

在回调函数中进行新的异步调用

[英]Make a new asynchronous call inside a callback function

I would like to use the Google AJAX Feed API to fetch multiple newsfeeds and display them on a webpage. 我想使用Google AJAX Feed API来获取多个新闻提要并将它们显示在网页上。

I can't use Google's FeedControl class because I want to control the way the items of the feed are displayed. 我无法使用Google的FeedControl类,因为我想控制供稿项的显示方式。 That leaves me with Google's Feed class as the only option. 剩下的唯一选择就是Google的Feed类。

I've got it to work with a single feed: provide a url and a callback function; 我已经将其与单个供稿一起使用:提供url和回调函数; process the results of the feed inside the callback function. 在回调函数中处理提要的结果。

My question: How do i fetch multiple feeds? 我的问题:如何获取多个提要? I would somehow have to make a new google.feeds.feed object inside the callback function and provide it with a new url and ... the same callback-function (?) 我将不得不以某种方式在回调函数中创建一个新的google.feeds.feed对象,并为其提供新的url和...相同的回调函数(?)

I never studied computer sciences, so this kind of recursion makes my head spin. 我从未学习过计算机科学,因此这种递归使我无法自拔。 Anyone can explain what I have to do? 任何人都可以解释我该怎么做?

Sure, you can do it that way, here's some pseudocode: 当然,您可以这样做,这是一些伪代码:

// 'feeds' is an array of the feed URLs
function grabFeeds(feeds) {
    var index;

    // We start with the first feed
    index = 0;

    // Kick off the process
    feedWorker();

    // Our "go get the next feed" function
    function feedWorker() {
        var feed;

        // Do we have any more?
        if (index < feeds.length) {
            // Yes, request it and bump our index
            // (You could combine these lines, but it's
            // clearer to keep them separate)
            feed = feeds[index];
            ++index;
            start_feed_download(feed, callback);
        }
    }

    // Our callback function
    function callback() {
        // ...do something with the result...

        // Kick of the next feed (feedWorker is defensive,
        // so we don't have to check index here)
        feedWorker();
    }
}

I don't know the Google Feed API, hence the placeholder start_feed_download function. 我不知道Google Feed API,因此不知道占位符start_feed_download函数。

What that does is start the process of grabbing the feeds via the grabFeeds function, which accepts an array of feeds. 要做的就是通过grabFeeds函数开始获取提要的过程,该函数接受一系列提要。 grabFeeds kicks off feedWorker , which initiates the first feed request, and then returns immediately (almost certainly before the first feed is retrieved). grabFeeds启动feedWorker ,后者会启动第一个提要请求,然后立即返回(几乎可以肯定的是,在检索第一个提要之前)。 callback processes the result, then asks feedWorker to kick off the next feed request (if there is one). callback处理结果,然后要求feedWorker启动下一个提要请求(如果有)。

The "magic" here is that feedWorker and callback are both closures , so even though grabFeeds has already returned, the index and feeds variables live on (in an object called an "execution context") for as long as anything references the things inside the execution context -- in our case, until callback and feedWorker are no longer referenced by Google's stuff. 这里的“魔术”是feedWorkercallback都是closures ,因此,即使grabFeeds已经返回, indexfeeds变量也feeds继续存在(在称为“执行上下文”的对象中),只要任何引用它内部的东西。执行上下文-在我们的例子中,直到Google的内容不再引用callbackfeedWorker为止。

Yes, you can do that okay, just make sure that whenever you do recursion you have two cases (a) the base case and (b) the recursive case. 是的,您可以做到这一点,只要确保每次进行递归时都有两种情况(a)基本情况和(b)递归情况。

Only in the second case does the function call itself, make sure you don't in the base case or else you will end up with an infinite recursion. 仅在第二种情况下,函数才调用自身,请确保您不在基本情况下,否则最终将导致无限递归。 For example, in this factorial function 0 is the base case ,everything else is the recursive case 例如,在此阶乘函数中,0是基本情况,其他都是递归情况

function fact(n) {
    if (n==0) { return 1; }
    else { return n * factorial(n-1) }
}

Alternatively, you could do them one at a time, ie. 或者,您可以一次执行一次,即。 start the first one, then start the second one, instead of having to wait for one to completely load before the second starts loading. 启动第一个,然后启动第二个,而不必等到第二个开始加载之前完全加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM