Skip to content

Instantly share code, notes, and snippets.

@che-wf
Last active February 22, 2017 18:28
Show Gist options
  • Select an option

  • Save che-wf/6dc188681a80db313d58129fe93f2314 to your computer and use it in GitHub Desktop.

Select an option

Save che-wf/6dc188681a80db313d58129fe93f2314 to your computer and use it in GitHub Desktop.
If you need to performa a function, wait, and perform it again, here's a nice loop to help with that.
function rateLimiter(thisArray, callback) {
var count = 0, // Loop count
rateNum = 10, // Items per loop
rateTime = 5000, // Time between loops
items = []; // Temp array
var batches = thisArray.length / rateNum;
function splitArray() {
items = thisArray.splice(0, rateNum);
console.log('batch ' + (count + 1) + ' out of ' + batches);
// Insert function here on var "items"
items.map((f, x) => {
magicMap(f);
});
// Insert function here on var "items"
setTimeout(() => {
count++;
if (count < batches) {
splitArray();
} else {
console.log('process complete');
if (callback) callback(null, true);
}
}, rateTime);
}
splitArray();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment