Skip to content

Instantly share code, notes, and snippets.

@skoshy
Last active July 30, 2019 16:59
Show Gist options
  • Save skoshy/4fe47d93a95ffc45b61e7e017b3aabda to your computer and use it in GitHub Desktop.
Save skoshy/4fe47d93a95ffc45b61e7e017b3aabda to your computer and use it in GitHub Desktop.
Allows you to delay execution of a function, very useful when console debugging
delayFunc = (func, {name = Date.now(), duration = 5000, interval = 1000} = {}) => {
let logger = (str) => console.log(`[${name}] ${str}`);
let iterationCount = 0;
let timer = setInterval(() => {
iterationCount++;
let finished = iterationCount * interval >= duration;
logger(`${iterationCount} / ${duration / interval}${finished ? ', Finished!' : ''}`);
if (finished) {
func();
return clearInterval(timer);
}
}, interval);
return { message: `[${name}] Starting timer`, timer };
};
delayFunc(() => { debugger; }); // Execute the debugger
delayFunc(() => {
document.querySelector('#some-thing').click();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment