Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send Ajax request on every 1s using JQuery?

How to send Ajax request on every 1s using JQuery ?

like image 983
Damir Avatar asked Nov 28 '22 10:11

Damir


2 Answers

You probably don't want to send a request every second as David already noted.

Therefore, using setInterval is a bad idea.

Instead consider doing something like this:

function doAjax() {

  $.ajax({
   ...
   complete: function() {
    setTimeout(doAjax,1000); //now that the request is complete, do it again in 1 second
   }
   ...
  });
}

doAjax(); // initial call will start rigth away, every subsequent call will happen 1 second after we get a response
like image 173
Martin Jespersen Avatar answered Dec 01 '22 01:12

Martin Jespersen


you can use setInterval, but setInterval ist not part of jQuery:

setInterval(function() {
    $.get(...);
}, 1000);
like image 29
felixsigl Avatar answered Dec 01 '22 00:12

felixsigl