for variable within .getJSON displayed wrong [duplicate]
问题 This question already has answers here : Calling an asynchronous function within a for loop in JavaScript (8 answers) JavaScript closure inside loops – simple practical example (44 answers) Closed 3 years ago . I have the following code (javascript/jQuery) $(function(){ for(var i=0;i<3;i++){ $.getJSON("https://api.twitch.tv/kraken/streams/esl_csgo", function(data){ console.log(i); console.log(data); }); } }); console log: 3 Object { stream: Object, _links: Object } 3 Object { stream: Object,