I
I
InterestingPerson2015-11-06 18:31:38
JavaScript
InterestingPerson, 2015-11-06 18:31:38

Why is the for loop not working?

There is an ajax request, in success I try to use the for loop, it does not work, that is, it does not go inside it. Foreach works, but I need for.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Gusev, 2015-11-06
@illi

Show your request code, it will be easier to suggest

V
Vitaly, 2015-11-06
@xytop

If you do something like
this in a loop and response is not an array but an object with properties, then response.length will be undefined and the loop will not be executed.
For example, try doing the following in the console:

var response = {key1: 'property1', key2: 'property2'};
console.log( response.length); // undefined
console.log( Object.keys(response).length); // 2

V
Vahe, 2015-11-07
@vahe_2000

Maybe you have something similar like this example?
Show the request code.

var bongo = [];
for (i = 0; i < 10; i++)
{

  (function(i)
    {
      $.ajax(
        {
          type: "GET",
          url: "http://localhost",
          data: queryString,
          success: function(request) {bongo[i] = request } 
        });  
    })(i);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question