JQuery throttling and AJAX request queuing

I interact with an API that allows one action in 5 seconds. However, I want all requests to end with a host. How can I queue and process requests that are created against the API using .ajax ()?

Significant Duty!

+7
source share
4 answers

You can do something in this direction

var requests = []; setInterval(function() { if(requests.length > 0) { var request = requests.pop(); if(typeof request === "function") { request(); } } }, 5000); // then anywhere you need to make an ajax request requests.push(function() { // ajax request here $.ajax({ url: "/foo", // some variable from outer scope success: function(a,b,c) { // handle it } }); }); 
+9
source

jQuery has . delay () and . queue () I suggest you check and read.

+3
source

I would highly recommend this plugin for throttling Ajax requests: http://benalman.com/projects/jquery-throttle-debounce-plugin/

+1
source

If you want to guarantee something, it’s best for you to access an environment other than the Internet β€” it is inherently unreliable. In either case, to try to provide, you could use the Javascript setTimeout to initiate the call later.

In addition, if you do not want to use any other third-party resources, you can learn the delay method by jQuery.

-2
source

All Articles