i have (slightly convoluted) set of functions populate dom info. begin, make call personalised json list of movies, in turn calls on omdb api each movie grab info want. want take pieces of information (in instance, genre) in order populate filter menu (click "comedy" if want see comedies, etc.).
i want create object containing genres , use populate menu. problem menu begins populating while initial ajax call still working. i've had go "when" don't seem getting right.
how can set instruction populates genre menu once of movie info sourced?
my (abridged) code far:
$(function(){ getmovielist(); }); function getmovielist() { $.ajax({ url: "movielist.json", type: "get", datatype: "json", success: function(data) { $.each(data, function(key, val) { var title = val.tolowercase().split(" ").join("+"); getmovieinfo(title); }); }, complete: function() { populategenre(); // not doing want.... } }); } function getmovieinfo(title) { $.ajax({ url: "https://www.omdbapi.com/?t=" + title + "&y=&plot=short&r=json", type: "get", crossdomain: true, datatype: "json", success: function(val) { if (!val.error) { $("#movie-list").append(...); genre.push(val.genre); } else {console.log(title)} // ... catch films not found on omdb } }); function populategenre() { console.log(genre); }
i want (for time being) console log couple of dud films , long, unwieldy array of genres. however, populategenre() console logs first, while getmovielist() , getmovieinfo() functions still working through long list of movies.
what should doing push populategenre() function? help!
the complete method on first ajax call not wait subsequent calls on other threads. you'll need use success method of subsequent calls know data loaded.
$(function(){ getmovielist(); }); function getmovielist() { $.ajax({ url: "movielist.json", type: "get", datatype: "json", success: function(data) { for(var i=0;i<data.length;i++){ var title = data[i].tolowercase().split(" ").join("+"); if(i == data.length - 1){ getmovieinfo(title, true); }else{ getmovieinfo(title, false); } } }); } }); } function getmovieinfo(title, islast) { $.ajax({ url: "https://www.omdbapi.com/?t=" + title + "&y=&plot=short&r=json", type: "get", crossdomain: true, datatype: "json", success: function(val) { if (!val.error) { $("#movie-list").append(...); genre.push(val.genre); } else {console.log(title)} // ... catch films not found on omdb if(islast){ populategenre(); } } }); function populategenre() { console.log(genre); }
Comments
Post a Comment