How can I limit a function to only run 10 times per second, but continue execution when new "spots" are available? This means we'd call the function 10 times as soon as possible, and when 1 second has elapsed since any function call we can do another call.
This description may be confusing - but the answer will be the fastest way to complete X number of API calls, given a rate limit.
Example:
Here is an example that loops through the alphabet to print each letter. How can we limit this to only printLetter
10 times per second? I still want to loop through all letters, just at the appropriate rate.
function printLetter(letter){
console.log(letter);
}
var alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "Z"];
// How can I limit this to only run 10 times per second, still loop through every letter, and complete as fast as possible (i.e. not add a hard spacing of 100ms)?
alphabet.forEach(function(letter){
printLetter(letter);
});
A good solution will not forcefully space out each call by 100ms. This makes the minimum run time 1second for 10 calls - when you could in fact do these (nearly) simultaneously and potentially complete in a fraction of a second.