javascript - $.get and saving global variables -


this question has answer here:

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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -