延迟JQuery函数直到另一个JQuery函数运行
我有一组(稍微复杂的)函数来用信息填充DOM。首先,我用一个电影列表调用一个个性化的JSON,该列表依次调用每个电影的OMDb API来获取我想要的信息。然后,我想获取这些信息的片段(在本例中为类型),以填充筛选菜单(如果您只想看喜剧,请单击“喜剧”,等等) 我想创建一个包含流派的对象,并用它来填充我的菜单。我的问题是,当我的初始AJAX调用仍在工作时,菜单开始填充。我曾尝试过“何时”,但似乎没有把握好 如何设置一条指令,使其仅在获取所有电影信息后填充流派菜单 迄今为止我的(节略)代码:延迟JQuery函数直到另一个JQuery函数运行,jquery,ajax,Jquery,Ajax,我有一组(稍微复杂的)函数来用信息填充DOM。首先,我用一个电影列表调用一个个性化的JSON,该列表依次调用每个电影的OMDb API来获取我想要的信息。然后,我想获取这些信息的片段(在本例中为类型),以填充筛选菜单(如果您只想看喜剧,请单击“喜剧”,等等) 我想创建一个包含流派的对象,并用它来填充我的菜单。我的问题是,当我的初始AJAX调用仍在工作时,菜单开始填充。我曾尝试过“何时”,但似乎没有把握好 如何设置一条指令,使其仅在获取所有电影信息后填充流派菜单 迄今为止我的(节略)代码: $(f
$(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 what I 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)} // ... to catch any films not found on OMDb
}
});
function populateGenre() {
console.log(genre);
}
我希望这(暂时)能帮助记录几部无聊的电影,然后是一系列冗长、笨拙的类型。但是,populateGenre()控制台首先记录日志,而我的getMovieList()和getMovieInfo()函数仍在处理一长串电影
我应该做什么来推回populateGenre()函数?谢谢你的帮助 第一个ajax调用上的complete方法不会等待其他线程上的后续调用。您需要使用后续调用的success方法来知道所有数据都已加载
$(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)} // ... to catch any films not found on OMDb
if(isLast){
populateGenre();
}
}
});
function populateGenre() {
console.log(genre);
}
$(函数(){
getMovieList();
});
函数getMovieList(){
$.ajax({
url:“movielist.json”,
键入:“获取”,
数据类型:“JSON”,
成功:功能(数据){
对于(var i=0;i第一个ajax调用上的complete方法不会等待其他线程上的后续调用。您需要使用后续调用的success方法来知道所有数据都已加载
$(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)} // ... to catch any films not found on OMDb
if(isLast){
populateGenre();
}
}
});
function populateGenre() {
console.log(genre);
}
$(函数(){
getMovieList();
});
函数getMovieList(){
$.ajax({
url:“movielist.json”,
键入:“获取”,
数据类型:“JSON”,
成功:功能(数据){
for(var i=0;iThanks@godmode!工作完美。太棒了,很乐意帮忙谢谢@godmode!工作完美。太棒了,很乐意帮忙