using script below i'm attempting create object called temptagarray gets populated tags on tumblr weblog , frequency. should end looking this:
{'performance': 10, 'installation': 5} i know object being created , looks correct (i can print out in each loop) can't figure out how use after/outside function i.e. @ bottom of script attempt document.write() out. global/local variable issue, return issue or need address in way?
<script type="text/javascript"> var temptagarray = {}; var tags; var tag; function loadposts () { var key = "api_key=9i4rzayqcbu1o5tsmzuyrlvxiqsnxkbiccjxnk5okz6g9pgdim"; var api = "https://api.tumblr.com/v2/blog/garrettlynch.tumblr.com/"; var retrieve_more = function (offset) { $.getjson(api + "posts?callback=?&filter=image&limit=20&offset=" + offset + "&" + key,function(data) { //for each item (post) in response $.each(data.response.posts, function(i, item) { //pull out posts tags tags = item['tags']; //loop through tags (i = 0; < tags.length; i++) { tag = tags[i]; //if tag exists in tag array if (temptagarray[tag]) { temptagarray[tag] = temptagarray[tag] + 1; } else { temptagarray[tag] = 1; } } }); if (data.response.posts.length == 20) { retrieve_more(offset + 20); } }); }; retrieve_more(0); } loadposts(); document.write(json.stringify(temptagarray)); </script> thanks in advance garrett
replace this:
if (data.response.posts.length == 20) { retrieve_more(offset + 20); } ...with this:
if (data.response.posts.length == 20) { retrieve_more(offset + 20); } else { document.write(json.stringify(temptagarray)); } the problem you're having that, despite document.write(...) command being located below ajax call in code, ajax call asynchronous , callback invoked asynchronously well. basically, document.write(...) being invoked long before you've had chance interact temptagarray variable in ajax callback.
Comments
Post a Comment