Skip to content

Instantly share code, notes, and snippets.

@jokeyrhyme
Last active June 29, 2019 08:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jokeyrhyme/9753904 to your computer and use it in GitHub Desktop.
Save jokeyrhyme/9753904 to your computer and use it in GitHub Desktop.
JavaScript polling
/**
* @param {Function} condition a function that returns `true` or `false`
* @param {Number} [interval=197] the amount of time to wait between tests
* @param {Function} callback a function to invoke when the condition returns `true`
*/
function waitFor(condition, interval, callback) {
'use strict';
if (condition && condition()) {
callback();
} else {
setTimeout(function () {
waitFor(condition, interval, callback);
}, interval || 197);
}
};
@jokeyrhyme
Copy link
Author

when Promises or Events are unavailable, this allows you to safely schedule code for the future

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment