Skip to content

Instantly share code, notes, and snippets.

@jtlindsey
Last active March 16, 2017 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtlindsey/ff5d5aa03e35f6e9befe11796ab58c4f to your computer and use it in GitHub Desktop.
Save jtlindsey/ff5d5aa03e35f6e9befe11796ab58c4f to your computer and use it in GitHub Desktop.
Pass any function and its arguments using rest parameter syntax to function that repeats that function.
/*
Sometimes i wan to run different functions multiple times without re-writing setInterval
and specifying speed, etc. This solution made that a little quicker.
*/
const looper = (runCount, i, speed, logit, fn, ...args) => {
let r = () => {
if (i++ >= runCount) {clearTimeout(si)};
logit ? console.log(fn(...args)) : fn(...args);
};
let si = setInterval(r, speed);
};
// runCount, 0, speed, logit, function, function_arg1, function_arg2, etc
looper(10, 0, 500, true, myfunction, "myfunction arg1");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment