Last active
July 30, 2019 16:59
-
-
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
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
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 }; | |
}; |
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
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