Skip to content

setTimeout with ajax

function ajax1(a, b, c){ 
  c = new XMLHttpRequest;
  c.open('GET', a);
  c.onload = b;
  c.send()
}

function handleData1(uu){
  console.log(10)
}
for (var i=0;i<5;i++){
setTimeout(ajax1("some_url", function(e){handleData1(this.response) }),1000)
}

I am stuck,I can’t use the setInterval function with ajax.

this if a simplified version of what I want my code to do.

As said in the code,I tried using setTimeout too but it didn’t work,javascript just ignores the funcion setInterval or setTimeout.

Answer

setInterval requires the first parameter to be a function.

for (var i=0;i<5;i++){
   setInterval(function(){
      ajax1("some_url", function(e){
         handleData1(this.response) 
      });
   }, 1000);
}