Skip to content

Instantly share code, notes, and snippets.

@Dibasic
Created August 28, 2019 15:05
Show Gist options
  • Save Dibasic/390a58019ebaa8d3575691eb7a3fd266 to your computer and use it in GitHub Desktop.
Save Dibasic/390a58019ebaa8d3575691eb7a3fd266 to your computer and use it in GitHub Desktop.
Wait until a condition becomes true to execute something
function waitUntil(condition, delay, maxAttempts, onSuccess, onFail) {
console.debug(`- - - waitUntil to run ${onSuccess.name} - trying ${condition.name} up to ${maxAttempts} times over up to ${delay * maxAttempts}ms`);
let attempts = 0;
function attempt() {
setTimeout(function() {
attempts++;
console.debug(`- - - - Attempting ${condition.name} to run ${onSuccess.name} (attempt ${attempts} of ${maxAttempts})...`);
if (condition()) {
let result = onSuccess();
console.debug(`- - - - - Success: ${onSuccess.name} returned ${result} after ${attempts * delay}ms`);
}
else if (attempts < maxAttempts) {
attempt();
}
else {
let result = onFail();
console.debug('- - - - Maximum attempts reached:');
console.debug(`- - - - - condition: ${condition.name} (currently: ${condition()})`);
console.debug('- - - - - delay per attempt: ' + delay);
console.debug('- - - - - attempts taken: ' + attempts);
console.debug('- - - - - onSuccess: ' + onSuccess.name);
console.debug(`- - - - - onFail: ${onFail ? `${onFail.name} returned ${result}` : 'not defined'}`);
}
}, delay);
}
attempt();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment