Javascript Global Variable Value Lost -
this question has reply here:
how homecoming response asynchronous call? 11 answersi'm having bit of problem regarding global variables in when nail block of code:
if(done === true){ console.log(songlist); }
the values of done , songlist gone, if output them console.log after assign done=true carry expected values (songlist built in function run eval statement). can explain why is?
var song = {name:"", price:"", artist:"", image:""}; var done = false; var songlist = []; function runjson(network,searchtype){ var url = returnurl(network,searchtype,$('#song_field').val().split(' ').join("+")); $.getjson(url, function(data){ if(document.getelementbyid("box") !== "undefined"){ clearscreen(); createcols(); } eval(network + "(data);"); done = true; }); if(done === true){ console.log(songlist); } }
done = true
getting assigned in callback $.getjson. run after test if(done === true)
.
more $.getjson here http://api.jquery.com/jquery.getjson/ - notice 'success' callback.
if did:
function runjson(network,searchtype){ var url = returnurl(network,searchtype,$('#song_field').val().split(' ').join("+")); $.getjson(url, function(data){ if(document.getelementbyid("box") !== "undefined"){ clearscreen(); createcols(); } eval(network + "(data);"); done = true; if(done === true){ console.log(songlist); } }); }
then see songlist
javascript variables global-variables
No comments:
Post a Comment