Created
March 4, 2024 21:34
-
-
Save broskisworld/91ed80d32afbf6c7029594b625e3d839 to your computer and use it in GitHub Desktop.
Wait for certain conditions to arise before calling callback function, with wait interval and optional timeout
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
function waitFor(condition, callback, waitMs = 100, timeoutMs = -1) { | |
if (condition()) { | |
callback(); | |
} else { | |
let interval = setInterval(() => { | |
if (condition()) { | |
clearInterval(interval); | |
callback(); | |
} | |
}, waitMs); | |
if(timeoutMs > 0) { | |
setTimeout(() => { | |
clearInterval(interval); | |
}, timeoutMs); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment