0

I'm trying to populate an array in JavaScript using an anonymous function in the jQuery getJSON() function as follows.

$(document).ready(function() {

    function Link(url, title) {
        this.url = url;
        this.title = title;
    }
    var links = [];

    $.getJSON("http://reddit.com/r/programming/.json?jsonp=?", function(data) {
        $.each(data.data.children, function(i, item) {
            var title = item.data.title;
            var url = item.data.url;

            links.push(new Link(url, title));
        })
    });

    for(var i=0; i< links.length; i++) {
        var output = "<a href='" + k + "'>" + links[k] + "</a>";
        $('<p>' + link + '</p>').appendTo('#content');
    }

});

But, when I hit the for loop, the links array shows up empty. What's going on here?

2
  • 2
    have you tried putting a break point inside the getJSON call back and the one inside the for? see which gets hit first? Commented Jun 5, 2012 at 3:33
  • 1
    Try moving the function declaration outside the document.ready Commented Jun 5, 2012 at 3:34

3 Answers 3

3

Try that :

    $(document).ready(function() {

        function Link(url, title) {
            this.url = url;
            this.title = title;
        }

        $.getJSON("http://reddit.com/r/programming/.json?jsonp=?", function(data) {
            var links = [];
            $.each(data.data.children, function(i, item) {
                var title = item.data.title;
                var url = item.data.url;

                links.push(new Link(url, title));
            })
            for(var i=0; i< links.length; i++) {
                var output = "<a href='" + k + "'>" + links[k] + "</a>";
                $('<p>' + link + '</p>').appendTo('#content');
            }
        });


    });

Your loop was probably executed before your callback ;)

Sign up to request clarification or add additional context in comments.

4 Comments

"Probably" - With an async call the loop would definitely be executed before the callback.
It is an asynchronous call, so nothing is guaranteed in theory. But I agree, on the practice, it will be always executed after .
Thank you for this. Is there a way to implement this such that execution blocks until all of the json data has been pulled? I'd like to be able to pull from many json sources and populate arrays of data (this was just my first example) and then do work on the various filled arrays.
(Not to start an argument, but) I'm not aware of any browsers that run JS on more than one thread. This means any currently executing code will always complete before any async callbacks.
3

That's because $.getJSON is an asynchronous method. The code execution continues even after $.getJSON and reaches the for loop, by which time, your async request hasn't completed yet. You should move the loop within $.getJSON.

Comments

1

This jsFiddle http://jsfiddle.net/cArYg/2/ shows the iteration occuring before the getJson callback

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.