Skip to content

Instantly share code, notes, and snippets.

@thelbouffi
Created October 7, 2016 11:46
Show Gist options
  • Save thelbouffi/9540a628848fd352101a0fff3ab2ebd7 to your computer and use it in GitHub Desktop.
Save thelbouffi/9540a628848fd352101a0fff3ab2ebd7 to your computer and use it in GitHub Desktop.
Looping with delay
// Loop for doesn't work
/*
for (var i=1; i < 5; i++) {
setTimeout(function () {
console.log('test');
}, 3000);
}
*/
// Solution
(function delayLoop(i){
setTimeout(function(){
console.log('test');
if(i--) delayLoop(i);
},3000);
})(5);
// each 3s we have an iterartion for 'i' time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment