The problem is that in each closure you use the same variable i - which changes. To keep the value of the variable unchanged - you need to create a new context by calling an anonymous function.
Here is one way:
for (var i=1; i<=100; i++) !function(i) { // $.post(... и т.д. }(i);
What kind of magic is going on here?
function(i) { ... } is the declaration of an anonymous function that takes a parameter i . This parameter hides the external variable i .
function(i) { ... }(i) is a call to the declared anonymous function, passing the value of the variable i as a parameter to it. Now inside the function, the parameter i is equal to the value that variable i at the time of the call. Now the cycle can go further, the value of the variable i will change to 101 - but inside the anonymous function the value of the parameter i will remain unchanged.
!function(i) { ... }(i) is one of the ways to write the last version so that the interpreter understands that this is an expression. The fact is that if a string starts with the word function , the interpreter will consider it a definition of a naming function, and there will be a syntax error. To avoid this, any operation is added to the expression.
The following design options are also allowed:
~function(i) { ... }(i)+function(i) { ... }(i)-function(i) { ... }(i)1/function(i) { ... }(i)(function(i) { ... })(i)(function(i) { ... }(i))
Another option is to wrap in an anonymous function not the entire loop body, but only the callback function passed to $.post :
for (var i=1; i<=100; i++) $.post("http://192.168.100.67/vtnuft/group"+i+"/check.php", function (i) { return function (responce) { // ... } }(i))
Here the anonymous function is not at the very beginning of the line - and therefore no tricks are required. Choose the one you like.
iis 101 - Grundy