Forum Discussion
This code can almost certainly not work, at least not in the way you provided the example, so giving you advice is hard here...
1. what are your "ideas"? are they integer ids, or JSON objects or something else?
2. if so, do you process each idea separately, if so, where is the $.each() loop?
3. You send a POST request and use a URL with GET parameters...just sayin'...
4. And of course you're getting a https://httpstatuses.com/429 error like this, no properly configured server should allow something like this, otherwise every 10 year old could DoS that server...
That means you have to properly set up a request queue that doesn't overwhelm the server with 1000 requests in less than a second... (use console.log() to see how quickly your code wants to send requests...), you can do that in various ways, I'm usually doing such things with a modified version of the ajaxQueue jQuery Plugin form here, it uses promises/deferred objects and I custom-added a delay option as I'm also using it to throttle requests sometimes, the modified plugin code is included below and after plugin code you find an example that you can run yourself (open the inspector > console to see what it does, but basically it delays each request for 1000ms = 1s):
(function($) { // jQuery on an empty object, we are going to use this as our Queue var ajaxQueues = [$({})]; $.ajaxQueue = function( ajaxOpts, queueIndex ) { var jqXHR, dfd = $.Deferred(), promise = dfd.promise(), ajaxQueue; // allow multiple queues that run independently of each other // specify the queue to be used like this (index can be ommited!): // $.ajaxQueue({<ajaxOpts>}, <int|queueIndex>) queueIndex = queueIndex || 0; // get queue index if specified ajaxQueue = ajaxQueues[queueIndex]; if ( ajaxQueue == undefined || !(ajaxQueue instanceof $) ) { ajaxQueue = ajaxQueues[queueIndex] = $({}); } // if there is no ajax request return an empty 200 code if ( typeof ajaxOpts == "undefined" ) { return $.Deferred(function() { this.resolve( [ '', '200', jqXHR ] ); }).promise(); } // run the actual query function doRequest( next ) { setTimeout(function() { jqXHR = $.ajax( ajaxOpts ); jqXHR.done( dfd.resolve ) .fail( dfd.reject ) .then( next ); }, ajaxOpts.delay||0); } // queue our ajax request ajaxQueue.queue( doRequest ); // add the abort method promise.abort = function( statusText ) { // proxy abort to the jqXHR if it is active if ( jqXHR ) { return jqXHR.abort( statusText ); } // if there wasn't already a jqXHR we need to remove from queue var queue = ajaxQueue.queue(), index = $.inArray( doRequest, queue ); if ( index > -1 ) { queue.splice( index, 1 ); } // and then reject the deferred dfd.rejectWith( ajaxOpts.context || ajaxOpts, [ promise, statusText, "" ] ); return promise; }; return promise; }; })(jQuery); $(function() { // just for demo purposes, this would be your $.each()... for (var i=0; i<10;i++) { $.ajaxQueue({ url: 'https://httpbin.org/get', delay: 1000 }).done(function(data) { console.log('success'); console.log(data); }).fail(function() { console.log('failed'); }); } });
luk Nice points.
You can use async: false instead of delay parameter. Next call to the endpoint would not be trigger until first call's response comes at the component. It would be more efficient and reliable.
for (var i=0; i<10;i++) { $.ajaxQueue({ url: 'https://httpbin.org/get', async: false,
data: {} }).done(function(data) { console.log('success'); console.log(data); }).fail(function() { console.log('failed'); }); }
- luk8 years agoBoss
VikasB yeah, but async (afaik) is mainly about "outsourcing" the AJAX calls to background tasks, e.g .make them non-UI-blocking, has nothing to do with throttling the requests, e.g. you will still overwhelm the server and trigger a 429 Error when async is set to false, delay is about dealing with rate limits, e.g. for OPs problem that is what is needed to not trigger the Firewall... DougS for that purpose it would be interesting what the rate limit is for endpoints?