Scope of variables inside of javascript

Posted by wcpro on Stack Overflow See other posts from Stack Overflow or by wcpro
Published on 2010-04-19T05:41:13Z Indexed on 2010/04/19 5:43 UTC
Read the original article Hit count: 238

Filed under:
|

I have the following code sample that im trying to wrap my head around

        $(document).ready(function () {

        test("load json", function () {

            length = 0; // length = 0

            $.getJSON("plugins/form.json", function (data) {
                length = data.fields.length; // length = 4
            });

            ok(length == 4, "length = " + length.toString()); // length = 0? wtf?
        });
    });

the 'length' variable does not persist when the $.getJSON runs. I cant figure out if its because its asynchronous or because the variable is out of scope.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript