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.
If the getJSON method is running asynchronously, then the ok(… line will likely execute before the getJSON returns…thus length will always be 0…
could you not do this…
that way it calls the ok function once the getJSON has returned with data…