Last active
December 13, 2017 12:42
-
-
Save oliverfoster/9bf9d710bd9ae712ac30de3760f48e15 to your computer and use it in GitHub Desktop.
waitUtil
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 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