基本的Javascript变量范围

时间:2016-05-27 08:35:27

标签: javascript variables

所以我正在努力创建一个可以返回这些twich用户的应用程序

  ["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

这就是我得到的:

“undefined正在播放”

所以它似乎没有吸引用户。感谢

1 个答案:

答案 0 :(得分: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