Ajax和for循环凌乱的结果

时间:2017-10-07 22:43:03

标签: javascript jquery

想知道为什么在这段代码中所有.ajax结果都被塞进了最后一个div?

$(document).ready(function() {
  var nicks = ["com2ususa", "ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
  for (var i = 0; i < nicks.length; i++) {
    var ni = nicks[i];
    $("body").append("<div id=" + ni + "></div>");
    $.ajax({
      url: "https://wind-bow.glitch.me/twitch-api/users/" + ni,
      success: function(result) {
        $("#" + ni).append("<img width='50px' src='" + result.logo + "'' />");
        $("#" + ni).append(result.display_name);
      }
    });
  };

});

0 个答案:

没有答案