Skip to content

Instantly share code, notes, and snippets.

@oliverfoster
Last active December 13, 2017 12:42
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 oliverfoster/9bf9d710bd9ae712ac30de3760f48e15 to your computer and use it in GitHub Desktop.
Save oliverfoster/9bf9d710bd9ae712ac30de3760f48e15 to your computer and use it in GitHub Desktop.
waitUtil
function waitUntil(options) {
options = options || {};
options.timeout = options.timeout || 1000;
var elapsed = 0;
var handle = setInterval(function() {
if (options.rule()) {
if (typeof options.success === "function") options.success();
if (typeof options.complete === "function") options.complete();
return clearInterval(handle);
}
elapsed+=100;
if (elapsed > options.timeout) {
if (typeof options.fail === "function") options.fail();
if (typeof options.complete === "function") options.complete();
return clearInterval(handle);
}
}, 100);
}
waitUntil({
rule: function() { return true }, // only success on true
timeout: 1000, // wait for 1second
success: function() { /* do this */ }, // do this on success
fail: function() { /* do this */ }, // do this after timeout
complete: function() { /* do this */ } // do this after timeout or success
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment