Created
December 29, 2017 12:10
-
-
Save xhawk/7738a9866ea3b1eea81f41664bf134d6 to your computer and use it in GitHub Desktop.
Repeat a function n times in 1 second or bail out if condition is met
This file contains 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
const repeat = (fn, cond, counter) => { | |
console.log(counter); | |
if (counter == 0 || cond(1)) { | |
return; | |
} else { | |
setTimeout(() => { | |
fn(); | |
repeat(fn, cond, counter - 1); | |
}, 1000); | |
} | |
}; | |
const cond = (x) => x == 1; | |
repeat(() => console.log("Hei"), cond, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment