How to send an Ajax request for every 1 using jQuery?

How to send an Ajax request for every 1 using jQuery?

+4
source share
5 answers

You probably do not want to send a request every second, as David already noted.

Therefore, using setInterval is a bad idea.

Instead, do the following:

 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 
+17
source

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

 setInterval(function() { $.get(...); }, 1000); 
+6
source

The interval of 1 second is small enough, and you can run the second request before receiving a response to the first request . Therefore, you must either start the next request after receiving the previous one (see Martin Jespersen's proposal), or save jqXHR from the previous $.ajax request in a variable and use it to abort the previous request (see the example here )

+1
source
 setInterval(myAjaxCall, 1000); 

Obviously, in the myAjaxCall () function, you will do whatever you want with jquery.

0
source

if you use the setInterval method, you can crush the browser because setInterval does not wait for the ajax function to complete, if the server is slow or the user connection speed is slow, this may be the reason for running several ajax requests the same time

0
source

Source: https://habr.com/ru/post/1343748/


All Articles