$ .getJSON在for循环中调用行为不端 [英] $.getJSON calls misbehaving inside a for loop

查看:137
本文介绍了$ .getJSON在for循环中调用行为不端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是对我的问题的描述:

Here's a description of my problem:

我有一个包含某些flickr照片的ID的数组。
为每个id,我需要制作两个嵌套的apiCalls(一个用于flickr photosets api,一个用于photoinfo api)并在我的页面中添加一些html

i have an array containing ids for some flickr photosets. for each id, i need to make two nested apiCalls (one to flickr photosets api, one to photoinfo api) and add some html to my page

结构是:

for(var i=0; i<ARRAY_ID.length; i++){

    $.getJSON(apiCall, function(data) {
         // some processing here

         $.getJSON(apiCall2,function(data){
              //other processing here

每个apiCall是一个字符串,包含对photosets api(apiCall)的正确调用,然后是在所有这些处理之后,在apiCall2块中,我将一些html附加到一个div(这导致与ARRAY_ID一样多的块元素)photoInfo api(apiCall2)

each "apiCall" is a string containing the correct call to the photosets api (apiCall) and then for the photoInfo api (apiCall2)

长度)。

我有奇怪的行为:所有附加的块元素包含相同的文本,图片和链接。它以预期的数字打印出来,但是内容与所有内容相同。我想是的for循环不等待$ .getJSON完成。我怎样才能实现这一目标?我严格要求在处理下一个json请求之前完成每个json请求!谢谢大家!

There's where i have the strange behaviour: all appended block elems contain the same text and picture and links. It prints them out in the expected number, but the content is the same throu all of them. I guess it's the for loop not waiting for the $.getJSON to finish. How can i achieve this? I strictly need each json request to be finished before the next one is processed! Thanks all!

推荐答案

您似乎有2个不同但相关的问题。

You seem to have 2 different, but related, issues going here.


所有附加的块元素包含相同的文本,图片和链接。它打印出预期的数字,但内容与所有内容相同。

all appended block elems contain the same text and picture and links. It prints them out in the expected number, but the content is the same throu all of them.

如果你引用<$回调中的c $ c> i ,您需要将其包含在。否则,当 $。getJSON 异步时,的将在调用回调之前完成,并且 i 将始终是循环中的最后一个值 - ARRAY_ID.length

If you're referencing i within the callbacks, you'll need to enclose it within the for. Otherwise, with $.getJSON being asynchronous, the for will finish before the callbacks are called and i will always be the last value in the loop -- ARRAY_ID.length:

for(var i = 0; i < ARRAY_ID.length; i++) {
    (function (i) {
        // work with the local `i` rather than the shared `i` from the loop
    })(i);
}








我想这是for循环而不是等待$ .getJSON完成。我怎样才能实现这一目标?我严格要求在处理下一个json请求之前完成每个json请求!

I guess it's the for loop not waiting for the $.getJSON to finish. How can i achieve this? I strictly need each json request to be finished before the next one is processed!

你不能得到 for 等待每组 $。getJSON 完成。但是,您可以使用 for 循环和闭包来构建队列,等待您明确告诉 next 继续:

You can't get the for to "wait" for each set of $.getJSON to finish. You can, however, use the for loop and closures to build a queue, which waits until you explicitly tell it next to continue:

var focus = $(...); // either match an existing element or create one: '<div />'

for(var i = 0; i < ARRAY_ID.length; i++) {
    (function (i) {
        focus.queue('apicalls', function (next) {
            $.getJSON(apiCall, function(data) {
                 // some processing here

                 $.getJSON(apiCall2,function(data){
                      //other processing here

                      next(); // start the next set of api calls
                 });
            });
        });
    })(i);
}

focus.dequeue('apicalls');

这篇关于$ .getJSON在for循环中调用行为不端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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