Created
December 30, 2017 03:35
-
-
Save rdillmanCN/4eadb4e7738088b7f2d9a60407e0366a to your computer and use it in GitHub Desktop.
Checks for an elements existence within a RAF within a promise.
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
/** | |
* Checks for an elements existence within a RAF within a promise. | |
* | |
* @param {string} selector - The element you wish to find. Defaults to 'body'. | |
* @param {string} target - The parent element to search within. Defaults to document. | |
* @return {Promise} Resolves when the element exists. | |
*/ | |
function elementReady(selector, target) { | |
var options = { | |
target: target || document, | |
selector: selector || 'body' | |
} | |
return new Promise(function promise(resolve) { | |
(function check() { | |
var el = options.target.querySelector(options.selector); | |
if (el) { | |
resolve(el); | |
} | |
requestAnimationFrame(check); | |
})(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment