-1

I am loading a 100k JSON file - it seems that even though I set the 'usersJSON' variable from a done function, the file has not in fact fully load. Firefox always fails and the console logs '{"readystate:1"}" - of course, what's needed here is 4.

Chrome does better but occasionally fails also. Smaller test files load perfectly. Anyone got any ideas here?

users.init();

users = { 

    usersJSON:"",

    init:function() {

        $.getJSON("userJSON.json",function() {

        }).done(function(data) {
            users.usersJSON=data;
        });
        users.processUsers()

    },
    processUsers:function() {
        var thisLog=JSON.stringify(users.usersJSON);
        console.log(thisLog);
    }
}
Charles Wyke-Smith
  • 2,479
  • 3
  • 17
  • 16
  • 2
    `users.processUsers()` is being executed too early. Move it to inside the done callback. – Kevin B Sep 11 '13 at 21:13
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – James Montagne Sep 11 '13 at 21:15

1 Answers1

1

Yep... Kevin B is right. Your code would work if you called processUsers from inside the "done" handler.

users.init();

users = { 

    usersJSON:"",

    init:function() {

        $.getJSON("userJSON.json",function() {

        }).done(function(data) {
            users.usersJSON=data;
            users.processUsers()
        });


    },
    processUsers:function() {
        var thisLog=JSON.stringify(users.usersJSON);
        console.log(thisLog);
    }
}
James Montagne
  • 77,516
  • 14
  • 110
  • 130
Rod Hartzell
  • 420
  • 3
  • 9