我試圖讓一些直播流的在線狀態和觀衆人數。我從AJAX獲取來自我網站的對象數組。然後我遍歷數組並向這些對象添加值。問題是,當我在控制檯中記錄這些對象的內容時,我發現新創建的密鑰,但是當我嘗試記錄該密鑰的值時,它說它是未定義的。jQuery操縱對象內.each()
$.each(livestreams, function() {
if(this.provider === 'TwitchTV') {
$.ajax({
url: 'http://api.justin.tv/api/stream/list.json?channel=' + this.channel.toLowerCase(),
dataType: 'jsonp',
jsonp: 'jsonp',
success: $.proxy(function (stream) {
this.live = true;
this.count = stream[0].channel_count;
}, this)
});
} else {
$.ajax({
url: 'http://api.own3d.tv/liveCheck.php?live_id=' + this.channel,
dataType: 'xml',
success: $.proxy(function (stream) {
this.live = stream.find('isLive').text();
this.count = stream.find('liveViewers').text();
}, this)
});
}
console.log(this);
/* returns
channel: "garenatw"
count: 8237
id: "3"
live: true
name: "garena"
provider: "TwitchTV"
username: "grifon"
__proto__: Object
(of course, only for this specific object)
*/
console.log(this.live); // returns undefined
});
你可以發佈[jsfiddle](http://jsfiddle.net/)嗎? –
http://jsfiddle.net/almirsarajcic/C4Jur/ 對不起,我試圖用HTML格式化它,但它沒有按照我的預期工作。 –
歡迎來到** async **的精彩世界!你不能那樣做。 – SLaks