Created
October 7, 2016 11:46
-
-
Save thelbouffi/9540a628848fd352101a0fff3ab2ebd7 to your computer and use it in GitHub Desktop.
Looping with delay
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
// 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