It is necessary to implement a timeout of 1 second in the loop. Tried through setTimeout, but did not go

for(i=0;i<ln.length;i++) { // Здесь нужен таймаут в 1 секунду var req="https://api.exmpent.com/?user = ln[i]" $.ajax({ url : req, type : "GET", dataType : "jsonp", success : function(msg){ console.log(msg.response); } }); } 

Reported as a duplicate at Grundy. javascript Nov 15 '17 at 12:35 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • How many times does the cycle repeat? (+ error in the condition of the cycle) - Mykola Kіkec
  • Corrected the mistake. Repeats 80-350 times - Sonfire
  • 2
    What does setInterval do not suit you? - teran
  • Here is another similar question: ru.stackoverflow.com/q/35579/183314 - mymedia
  • setInterval code hangs and setTimeout does not work - Sonfire

1 answer 1

if you need to send N ajax requests with a certain interval, then you can easily use the setInterval() function and complete its execution with clearInterval() when you reach the desired number of iterations.

 var idx = 0; var iid = null; function doSomething(){ $("<li>").text("$.ajax(): " + idx++).appendTo("ul"); // отправить $.post/$.get if(idx == 20){ // конец цикла clearInterval(iid); } } iid = setInterval(doSomething, 1000); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul></ul>