Skip to content

Instantly share code, notes, and snippets.

@xhawk
Created December 29, 2017 12:10
Show Gist options
  • Save xhawk/7738a9866ea3b1eea81f41664bf134d6 to your computer and use it in GitHub Desktop.
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
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