Skip to content

Instantly share code, notes, and snippets.

@che-wf
Created July 22, 2016 16:55
Show Gist options
  • Select an option

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

Select an option

Save che-wf/3de6dca2e86f83f441fa74dbd2e8458b to your computer and use it in GitHub Desktop.
If you're ever needing to run a loop and break for a set period of time before continuing a loop, this should help do that
var myArr = [],
count = 0,
batches = 0,
newArr = [];
for (var i = 0; i < 103; i++) {
myArr.push(i);
if (myArr.length > 9) {
batches = Math.ceil(myArr.length / 10);
}
}
console.log('total batches = ' + batches);
function splitArray() {
// document.getElementById('text').innerHTML = newArr;
newArr = myArr.splice(0, 10);
// console.log('batch ' + count + ' out of ' + batches);
// console.log(newArr);
setTimeout(function() {
console.log('batch ' + (count + 1) + ' out of ' + batches);
count++;
console.log(newArr);
if (count < batches) {
console.log('waiting...')
} else {
console.log('process complete');
}
if (count < batches) {
splitArray();
}
}, 3000);
}
splitArray();
console.log('--------------');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment