The problem with nested $ .getJSON


$.getJSON('http://twitter.com/followers/ids.json?screen_name=/…'+ query1 + '&callback=?',
  function(data) {
    alert('JSON data string 1 is: '+data); 
    $.getJSON('http://twitter.com/followers/ids.json?screen_name=/…'+ query2 + '&callback=?', 
      function(data1) {
        alert('JSON data string 2 is: '+data1); 
        f2=data1; 
        f1=data; 
        for(var i=0; i "less than" f1.length; i++)
        {
          for(var j=0; j "less than" f2.length; i++)
          {
            if (f1[i] == f2[j])
            {
              common[c]=f1[i];
              c+=1;
            }
          }
        }
        $('#content').append(''+common.length+'');//this line is not working though....... 
    });
});

In this piece of string

$('#content').append(''+common.length+'');

does not show the conclusion on which it hangs

Any help would be appreciated.

thanks

+3
source share
5 answers

Do not insert calls. You also avoid some memory issues when javascript maintains a copy of all local variables for each anonymous function. By making two calls separately, you can also make both calls at the same time, rather than sequentially.

This is the best method if I understand your purpose. Plus, by polling gotA and gotB you can even make a nice little "wait for A notification awaiting B" for users.

: .

var gotA, gotB;
var followingA, followingB;
function getCommonFollowers(user1, user2)
{
    gotA = false;
    gotB = false;
    jQuery.getJSON('http://twitter.com/followers/ids.json?screen_name=/…'+ user1 + '&callback=?', gotFollowersOfA );
    jQuery.getJSON('http://twitter.com/followers/ids.json?screen_name=/…'+ user2 + '&callback=?', gotFollowerOfB );
}
function gotFollowersOfA(data)
{
    followingA = data;
    gotA = true;
    if (gotB) {
        compareFollowersAB();
    }
}
function gotFollowersOfB(data)
{
    followingB = data;
    gotB = true;
    if (gotA) {
        compareFollowersAB();
    }
}
function compareFollowersAB()
{
    f2=followingA; 
    f1=followingB; 
    for(var i=0; i < f1.length; i++) {
        for(var j=0; j < f2.length; j++) {
            if (f1[i] == f2[j]) {
                //console.log("Adding f1[i]");
                common.push(f1[i]);
            }
        }
    }
    $('#content').append(''+common.length+'');
}
+6

j.

, ..

, , - script.

jsbin.com pastebin.me, .

+2

Great Turtle, , , :

        var query1 = 'user1';
        var query2 = 'user2';
        var data;
        var data1; 
        var f1;
        var f2;
        var common = [];
        var c = 0;

        $(document).ready(function() {
            $.getJSON('http://twitter.com/followers/ids.json?screen_name=' + query1 + '&callback=?', 
                function(data) { 
                console.log('JSON data string 1 is: ' + data); 
                $.getJSON('http://twitter.com/followers/ids.json?screen_name=' + query2 + '&callback=?', 
                    function(data1) { 
                    console.log('JSON data string 2 is: ' + data1); 
                    f2=data1; 
                    f1=data; 
                    for(var i=0; i < f1.length; i++) {
                        for(var j=0; j < f2.length; j++) {
                            if (f1[i] == f2[j]) {
                                //console.log("Adding f1[i]");
                                common.push(f1[i]);
                            }
                        }
                    }
                    $('#content').append(''+common.length+'');
                }); 
            });
        });

, 2- for, , ++ j ++, .

, console.log, , Firefox Firebug, console.log .

+1

'http://'?

$.getJSON('http://twitter.com...'

0

Declare a variable common outside of json functions.

0
source

Source: https://habr.com/ru/post/1712029/


All Articles