How to do an Ajax update every 10 seconds in jquery?

How to do an Ajax update every 10 seconds in jquery?

$.ajax({ type: "GET", url: options.feedUrl, dataType: "xml", async:options.sync, success: function(xml) { } 

For example, I am testing jquery above to get an RSS feed. So, how do you update RSS every 10 seconds so the user can see a new item in the feed?

+4
source share
3 answers

Create Interval

 var ResInterval = window.setInterval('myAjaxCall()', 60000); // 60 seconds var myAjaxCall = function() { $.ajax({ type: "GET", url: options.feedUrl, dataType: "xml", async:options.sync, success: function(xml) { // todo } }; 

To stop

 window.clearInterval(ResInterval); 
+7
source

I would avoid ajax synchronous call. This will do something else on the page, like freezing the animation. Of course, with asynchronous calls, you need to make sure that they do not start to overlap. In your SetInterval, you can simply set the lock:

 var doingAjax = false; setInterval(function() { if (!doingAjax) { doingAjax = true; jQuery.ajax({ ..., success: function() { doingAjax = false; ... } }); } }; 
+1
source

wrap it in a function and use setInterval ()

0
source

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


All Articles