循环不按预期工作

时间:2016-06-04 14:20:53

标签: javascript css

http://codepen.io/abdulahhamzic/pen/rLBoOj(CodePen演示)

这部分js代码让我感到困惑:

for (i=0; i<list.length; i++){
url = 'https://api.twitch.tv/kraken/streams/' + list[i];
urlChan = 'https://api.twitch.tv/kraken/channels/' + list[i];
directUrl = "https://www.twitch.tv/" + list[i];
$.getJSON(url, function(json) {
    if (json.stream !== null){
        $('#all').append('<div class="streamAreaOnline col-xs-12"><div class="col-xs-2"><img src="' + json.stream.channel.logo + '" class="img-circle"></div><div class="col-xs-10 cont-10"><div class="col-xs-10 col-sm-4"><a href="' + directUrl + '"><p class="name">' + json._links.self.substring(37, 38).toUpperCase() + json._links.self.substring(38, 700) + '</p></a></div><div class="col-xs-10 col-sm-4"><p class="otherInfo">' + json.stream.game + '<span class="status">: ' + json.stream.channel.status + '</span></p></div></div></div>');
        $('#online').append('<div class="streamAreaOnline col-xs-12"><div class="col-xs-2"><img src="' + json.stream.channel.logo + '" class="img-circle"></div><div class="col-xs-10 cont-10"><div class="col-xs-10 col-sm-4"><a href="' + directUrl + '"><p class="name">' + json._links.self.substring(37, 38).toUpperCase() + json._links.self.substring(38, 700) + '</p></a></div><div class="col-xs-10 col-sm-4"><p class="otherInfo">' + json.stream.game + '<span class="status">: ' + json.stream.channel.status + '</span></p></div></div></div>');
    }
    else{
        $.getJSON(urlChan, function(jsonTwo){
            if (!jsonTwo.logo){
                $('#all').append('<div class="streamAreaOffline col-xs-12"><div class="col-xs-2"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/No_image_available.svg/2000px-No_image_available.svg.png" class="img-circle"></div><div class="col-xs-10 cont-10"><div class="col-xs-10 col-sm-4"><a href="' + directUrl + '"><p class="name">' + jsonTwo._links.self.substring(38, 39).toUpperCase() + jsonTwo._links.self.substring(39, 700) + '</p></a></div><div class="col-xs-10 col-sm-4"><p class="notExist">Non-existing</p></div></div></div>');
                $('#offline').append('<div class="streamAreaOffline col-xs-12"><div class="col-xs-2"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/No_image_available.svg/2000px-No_image_available.svg.png" class="img-circle"></div><div class="col-xs-10 cont-10"><div class="col-xs-10 col-sm-4"><a href="' + directUrl + '"><p class="name">' + jsonTwo._links.self.substring(38, 39).toUpperCase() + jsonTwo._links.self.substring(39, 700) + '</p></a></div><div class="col-xs-10 col-sm-4"><p class="notExist">Non-existing</p></div></div></div>');
            }
            else{
                $('#all').append('<div class="streamAreaOffline col-xs-12"><div class="col-xs-2"><img src="' + jsonTwo.logo + '" class="img-circle"></div><div class="col-xs-10 cont-10"><div class="col-xs-10 col-sm-4"><a href="' + directUrl + '"><p class="name">' + jsonTwo._links.self.substring(38, 39).toUpperCase() + jsonTwo._links.self.substring(39, 700) + '</p></a></div><div class="col-xs-10 col-sm-4"><p>Offline</p></div></div></div>');
                $('#offline').append('<div class="streamAreaOffline col-xs-12"><div class="col-xs-2"><img src="' + jsonTwo.logo + '" class="img-circle"></div><div class="col-xs-10 cont-10"><div class="col-xs-10 col-sm-4"><a href="' + directUrl + '"><p class="name">' + jsonTwo._links.self.substring(38, 39).toUpperCase() + jsonTwo._links.self.substring(39, 700) + '</p></a></div><div class="col-xs-10 col-sm-4"><p>Offline</p></div></div></div>');
            }
        });
    }
});
}    

为什么每次迭代都不会更新urlChan字符串?

0 个答案:

没有答案