0

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, _links: Object }
3
Object { stream: Object, _links: Object }

isn't it true, that console.log(i); should log 0 in the first loop, 1 in the second and 2 in the third? But somehow it always returns 3 :(

orly
  • 164
  • 1
  • 7

4 Answers4

1

Try this

$(function(){
    for(var i=0;i<3;i++){
        (function (index) {
            $.getJSON("https://api.twitch.tv/kraken/streams/esl_csgo", function(data) {
                console.log(index);
                console.log(data);
            });
        })(i)
    }
});
Akshay Khandelwal
  • 1,570
  • 11
  • 19
  • This happens as the for loop executes completely before AJAX gets triggered and the state is maintained in the code for the variable **i**. Using closures and anonymous function to maintain the state of i during the execution of the for loop. – Akshay Khandelwal Aug 14 '16 at 17:43
  • thank you! it worked perfectly even tho I don't understand what you did. How is this procedure called ; is there literature about this? – orly Aug 14 '16 at 17:49
  • @orly Read the first link in the comments under your question. – Sebastian Simon Aug 14 '16 at 17:49
1

If you are using ecma6 then you can use let to avoid closure issue. It create block scoping.

$(function() {
    for (let i = 0; i < 3; i++) {

        $.getJSON("https://api.twitch.tv/kraken/streams/esl_csgo", function(data) {
            console.log(i);
            console.log(data);
        });
    }
});

otherwise,You can use IIFE

$(function() {

    for (var i = 0; i < 3; i++) {

        (function(i) {

            $.getJSON("https://api.twitch.tv/kraken/streams/esl_csgo", function(data) {
                console.log(i);
                console.log(data);
            });

        })(i)

    }
});

Similary,you can use bind.

RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
0

Can you display result in $.getJson success?

 $(function(){
            $.getJSON("https://api.twitch.tv/kraken/streams/esl_csgo", function(data){
              for(var i=0;i<3;i++){
                console.log(i);
                console.log(data[i]);
               }
            });
        });

Your i=3 because your loop does not wait $.getJson success. It is working and increment values without delay.

Jasmin Kurtic
  • 141
  • 12
-2

Try This

    $.ajaxSetup({
        async: false
    });

    $(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);
        });
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Parithiban
  • 1,656
  • 11
  • 16
  • He can "try this", but he should never use it. Making these Ajax requests synchronous is not necessary (at least in this case), and it may freeze the webpage while waiting for the responses. – blex Aug 14 '16 at 17:53