变量在for循环中没有变化 - JavaScript

时间:2016-04-13 04:27:11

标签: javascript variables for-loop scope

为什么输出每个feedName的名称相同(MedryBW)?我花了一段时间搞乱它,无法搞清楚。我希望它在循环的每次迭代中输出feed的名称,而不是一遍又一遍地重复相同的。感谢大家。

var feeds = ["Towellie", "TrumpSC", "TeamSp00ky", "TwitchPlaysPokemon", "Widgitybear", "AriaBlarg", "TheMexicanRunner", "OPNerd", "rabbitbong", "Wingsofdeath", "MedryBW"];

$(document).ready(function(){
    for(x = 0; x < feeds.length; x++){
        var feedName = feeds[x];
        $.getJSON('https://api.twitch.tv/kraken/streams/' + feeds[x] + '?callback=?', function(data) {
            if(data.stream === null){
                $('#feeds').append("<p>" + feedName + " is offline </p>");
            } else {
                $('#feeds').append("<p>" + feedName + " is streaming "  (data.stream.game) + "/<p>");
            }
        });
    }
});

3 个答案:

答案 0 :(得分:3)

因为回调函数运行得晚得多,而不是在循环中运行,并且它只是在循环结束后得到变量值(最后一个值),使用bind函数将变量传递给函数

var feeds = ["Towellie", "TrumpSC", "TeamSp00ky", "TwitchPlaysPokemon", "Widgitybear", "AriaBlarg", "TheMexicanRunner", "OPNerd", "rabbitbong", "Wingsofdeath", "MedryBW"];

$(document).ready(function() {
    for(x = 0; x < feeds.length; x++){
        var feedName = feeds[x];
        $.getJSON('https://api.twitch.tv/kraken/streams/' + feeds[x] + '?callback=?', function(feedName, data) {
            if (data.stream === null) {
                $('#feeds').append("<p>" + feedName + " is offline </p>");
            }else{
                $('#feeds').append("<p>" + feedName + " is streaming " + (data.stream.game) + "/<p>");
            }
        }.bind(this, feedName));
    }
});

答案 1 :(得分:0)

正如您在for循环中编写Ajax一样,成功回调在for循环执行后执行

var feeds = ["Towellie", "TrumpSC", "TeamSp00ky", "TwitchPlaysPokemon", "Widgitybear", "AriaBlarg", "TheMexicanRunner", "OPNerd", "rabbitbong", "Wingsofdeath", "MedryBW"];

$(document).ready(function() {
  feeds.forEach(function(feedName) {
    $.getJSON('https://api.twitch.tv/kraken/streams/' + feedName + '?callback=?', function(data) {
      if (data.stream === null) {
        $('#feeds').append("<p>" + feedName + " is offline </p>");
      } else {
        $('#feeds').append("<p>" + feedName + " is streaming " + (data.stream.game) + "/<p>");
      }
    });

  });
})

以上变更将起作用

答案 2 :(得分:0)

你可以通过。

实现这一目标
var feeds = ["Towellie", "TrumpSC", "TeamSp00ky", "TwitchPlaysPokemon", "Widgitybear", "AriaBlarg", "TheMexicanRunner", "OPNerd", "rabbitbong", "Wingsofdeath", "MedryBW"];

        $(document).ready(function(){
        var divText;
        for(x = 0; x < feeds.length; x++){
            var feedName = feeds[x];
            $.getJSON('https://api.twitch.tv/kraken/streams/' + feeds[x] + '?callback=?', function(data) {
          if(data.stream === null){
         divText = divText + '<p>'+feedName+' is offline </p>';
          }else{
         divText = divText + '<p>'+feedName+' is streaming '+(data.stream.game) +'</p>'
        }
        });
        }
     $('#feeds').append(divText); 
     //or $('#feeds').html(divText); 

        })