Last active
February 22, 2017 18:28
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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