What I'm trying to do sounds simple. I want to load various chunks of data into javascript via json and then use it in other functions (such as jquery autocomplete).
I just can't figure out how to delay the 'other functions' until the data is loaded.
eg
The alert shows townarray is empty.Code:$(document).ready(function(){ townarray = getTownArray(); alert("towns="+townarray); } function getTownArray() { var myarray = []; var url = "ajaxjson.php?gettownlist=1"; // get town data $.getJSON(url,function(data) { $.each(data, function(key,value) { myarray.push(value['description']); }); }); return(myarray); };
If I wrap the alert in a window.setTimeout, it is fine.
I've been going round in circles trying to delay the alert until the townarray is filled in. I've tried adding a callback to getTownArray, but it just wouldn't wait for the array to be filled in - is that the way to go?
Can anyone help please?



Reply With Quote


Bookmarks