javascript - $.get and saving global variables -
this question has answer here:
- how return response asynchronous call? 21 answers
var = 0; var requeststr ="https://api.flickr.com/services/rest/?method=flickr.interestingness.getlist&format=json&nojsoncallback=1&per_page=20&api_key=dc140afe3fd3a251c2fdf9dcd835be5c"; //flickr key. $.get(requeststr, function(data){ = data; }); console.log(a);
this prints out 0, im trying save data
parameter global variable a.
i've tried self invoking function , using 2nd function set a = data
, don't work.
honestly can't figure out.
you need have promise:
$.get(requeststr, function(data){ = data; }).always(function(){ console.log(a); });
because $.get()
method async call, response take time come , execution of js codes gets executed.
other might want take @ $.when()
.
Comments
Post a Comment