2016-05-27 29 views
1

所以我試圖讓一個應用程序,將返回是否每個twich用戶基本的JavaScript變量範圍

["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx",  "RobotCaleb", "noobs2ninjas"] 

是流的。這是我的代碼:

var users= ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"]; 
var progress =0; 
var streaming = []; 
$(document).ready(function() { 
console.log(window.progress); 
while (window.progress<8) { 


var url = 'https://api.twitch.tv/kraken/streams/' + window.users[window.progress] + '?callback=?'; 
$.ajax({ 
url: url, 
async: false, 
dataType: 'json', 
success: function(data) { 
if (data.stream) { 

window.streaming[window.progress]="streaming"; 

} 
//if closed 
else { 
window.streaming[window.progress]="not streaming"; 
}; 
//else closed 

console.log(window.progress); 
console.log(window.users[window.progress]); 
var html = "<p> <a href=\" https://www.twitch.tv/"+window.users[window.progress] +" \" >"+window.users[window.progress]+ "</a> is currently " +window.streaming[window.progress] + "</p>"; 
$("body").append(html); 
}}); 
// getjson closed 
     window.progress +=1; 
     }; 
// for loop closed 

}); 

//document ready closed 

,這就是我得到:

「未定義正流」

所以它似乎不拉用戶。由於

回答

0

你流陣列沒有初始化,所以這不能做,由於沒有0,1,...元就可以了

window.streaming[window.progress]="streaming"; //streaming.length == 0, streaming[0] == 'undefined' 

也許你想克隆users.length對它有一個索引

streaming = []; // length == 0 
streaming.length = users.length; // length == users.length