Skip to content

Instantly share code, notes, and snippets.

@broskisworld
Created March 4, 2024 21:34
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 broskisworld/91ed80d32afbf6c7029594b625e3d839 to your computer and use it in GitHub Desktop.
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
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