我一整天都在浏览论坛,但我不明白。
我正在调用 Justin.TV-API 来显示我的“关注”流的列表,然后执行嵌套调用来检查其中哪一个在线。
这是我想要使用的代码:
//Get Favorites as Object "favs"
var viewers;
$.getJSON('http://api.justin.tv/api/user/favorites/hubschrauber.json?jsonp=?', function(favs) {
$.each(favs, function(key, value) {
//For every Object, check if user is referenced in stream and therefore online
$.getJSON('http://api.justin.tv/api/stream/list.json?channel=' + favs[key].login + '&jsonp=?', function(streams) {
viewers = streams[0].channel_count;
});
$('#result').append(favs[key].title + ' ' + key + ': ' + favs[key].login + ' (' + viewers + ') Viewers<br />');
});
});
然后我遇到了 getJSON 和异步化的问题,并重写了我的脚本几次:
首先像这样(替换 $.each):
//Get Favorites as Object "favs"
var viewers;
$.getJSON('http://api.justin.tv/api/user/favorites/hubschrauber.json?jsonp=?', function(favs) {
for (var key in favs) {
//For every Object, check if user is referenced in stream and therefore online
$.getJSON('http://api.justin.tv/api/stream/list.json?channel=' + favs[key].login + '&jsonp=?', function(streams) {
viewers = streams[0].channel_count;
});
$('#result').append(favs[key].title + ' ' + key + ': ' + favs[key].login + ' (' + viewers + ') Viewers<br />');
}
});
然后像这样(用非异步ajax替换getJSON):
var favs;
var streams;
var viewers;
$.ajax({
url: 'http://api.justin.tv/api/user/favorites/hubschrauber.json?jsonp=?',
async: false,
dataType: 'json',
success: function(favs) {
for (var key in favs) {
$.ajax({
url: 'http://api.justin.tv/api/stream/list.json?channel=' + favs[key].login + '&jsonp=?',
async: false,
dataType: 'json',
success: function(streams) {
viewers = streams[0].channel_count;
}
});
$('#result').append(favs[key].title + ': ' + favs[key].login + ' (' + viewers + ') Viewers<br />');
}
}
});
在任何情况下,我都无法将观众变量(在嵌套 api 调用中编写,以确定流是否在线以及有多少观众)传递给
$('#result').append(favs[key].title + ': ' + favs[key].login + ' (' + viewers + ') Viewers<br />');
我的输出始终未定义:
Day[9]: day9tv (undefined) Viewers
FollowGrubby: followgrubby (undefined) Viewers
OneMoreGame.TV: onemoregametv (undefined) Viewers
我已经对附加和警报进行了多次检查 - JSON 响应是有效的。
谁能帮帮我?
非常感谢您抽出时间。
最佳答案
Ajax 是异步的,填充后需要访问viewers 变量的任何内容都需要位于ajax 请求的完整回调内部。
延迟对象将是这里的方法,我将制作一个小型演示。
更新:
http://jsfiddle.net/cMKkQ/
不需要延迟对象,只需稍微重组一下。
//Get Favorites as Object "favs"
$.getJSON('http://api.justin.tv/api/user/favorites/hubschrauber.json?jsonp=?', function(favs) {
$.each(favs, function(key, value) {
//For every Object, check if user is referenced in stream and therefore online
$.getJSON('http://api.justin.tv/api/stream/list.json?channel=' + favs[key].login + '&jsonp=?', function(streams) {
$('#result').append(value.title + ' ' + key + ': ' + value.login + ' (' + streams[0].channel_count + ') Viewers<br />');
});
});
});
更新:
另一种方法:
var favorites = [];
function processFavorites () {
$("#result").empty();
$.each(favorites,function(index,obj){
// here we populate the results div
$('#result').append(obj.title + ' ' + index + ': ' + obj.login + ' (' + obj.viewers + ') Viewers<br />');
});
}
function getFavorites () {
favorites = [];
$.getJSON('http://api.justin.tv/api/user/favorites/hubschrauber.json?jsonp=?', function(favs) {
var defArr = [];
$.each(favs, function(key, value) {
//For every Object, check if user is referenced in stream and therefore online
defArr.push($.getJSON('http://api.justin.tv/api/stream/list.json?channel=' + favs[key].login + '&jsonp=?', function(streams) {
favorites.push({title:value.title,key:key,login:value.login,viewers:streams[0].channel_count});
}));
});
$.when.apply($,defArr).always(function(){
processFavorites();
});
});
}
// this runs the process of getting and processing favorites. Run it when you want.
getFavorites();
但是,提要当前没有返回任何结果,我认为这与此代码无关。
关于jquery - $.getJSON、$.each 和变量范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9687274/