Restriction of requests per second
-
How can the number of requests be limited per second?
There's a violin:var q = items.reduce(function (prev, item) { return prev.then(function () { return $.ajax({ dataType: 'json', url: 'https://example.com/method/methodOne?callback=?' }).then(function (results_one) { console.log(results_one); return $.ajax({ dataType: 'json', url: 'https://example.com/method/methodTwo?callback=?' }).then(function (results_two) { console.log(results_two); }); }); }); }, $.when([]));
The number of requests should not exceed N (if the server makes a mistake).
-
- Consider the requests launched. Like before.
$.ajax()
Please.requestCount += 1
♪ However,requestCount
must be visible from all requests; - Before each request, check the counter didn't exceed that threshold. If we've surpassed, we're going to pause at 1 second and then we're gonna drop the counter.
Pause can be made about:
function sleep(duration) { var defer = $.Deferred(); setTimeout(function() { defer.resolve(); }, duration); return defer.promise(); }
Result of implementation
sleep()
to be returned after inside the function.then()
- Consider the requests launched. Like before.