0

In my javascript code I receive a value that tells me how many times I have to execute a piece of code but it executes just on time.

For example:

var qtt = 3;
for (var i = 1; i <= qtt; i++) {
    console.log(i);
    $.get( "/set-serie", function(seriedata)
    {
        console.log('test ' + i);
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

The result is:

1
2
3
test 4

So, this just execute the code one time instead 3. And I don't understand why it put 4 at the end.

user3242861
  • 1,839
  • 12
  • 48
  • 93
  • 4
    By the time the `$.get()` finishes, it is at the end of the loop and the variable has been incremented. – zero298 May 14 '18 at 17:29
  • It's also a code smell that the ajax call is the exact same call for each iteration. – Taplar May 14 '18 at 17:36
  • It's a duplicate of none of those questions. @zero298 turn your comment into an answer! –  May 14 '18 at 17:41
  • 4
    There should be a box with `3` on the left of `test 4` meaning that `test 4` has been logged `3` times. – ibrahim mahrir May 14 '18 at 17:44
  • 2
    @D_N your comment is confusing. You state his comment is an answer, and that it's not a duplicate... but the duplicate is that exact scenario. – Kevin B May 14 '18 at 17:49
  • @KevinB No, that's about a loop based on simple looping and an on-page event listener, and does not include an AJAX request, which is what is creating a problem for the OP. And previously there were three separate articles linked as duplicate, none of which were the same, which prompted my general comment. –  May 14 '18 at 17:53
  • 1
    @D_N This is absolutely a duplicate. It may a combination duplicate of many of the duplicates listed, but it is still a duplicate. The issue stems from not capturing the iteration counter into a context that won't be affected by the for loop. – zero298 May 14 '18 at 17:56
  • @zero298 If it's a combination of a bunch of pages which are not duplicate, it's not duplicate. –  May 14 '18 at 17:58
  • 1
    The one duplicate adequately covers it. We don't need several. – Kevin B May 14 '18 at 18:01
  • 1
    To me it seem that OP doesn't know how the console works, seeing test only printed once and so thinking the function only ran once, assuming @ibrahimmahrir is correct. While OP can benefit from reading the close as duplicate question, I disagree that is in fact a duplicate. – Musa May 14 '18 at 18:03
  • 1
    That just makes it an X/Y, it doesn't make it not a duplicate. – Kevin B May 14 '18 at 18:04
  • @KevinB If you already know it, it makes sense as an answer. If you don't, it doesn't. Not very helpful. –  May 14 '18 at 18:05
  • 1
    You could see the question as a duplicate, but only in a very abstract way. Also, besides the issue from the 'duplicate', OP asks why i is 4, where he expected 3. This is not explained in the 'duplicate'. – Hans Dash May 14 '18 at 18:09
  • Cast your vote. – Kevin B May 14 '18 at 18:10
  • 1
    @D_N then it's too localized. – zero298 May 14 '18 at 18:18

0 Answers0