Created
December 30, 2017 09:19
-
-
Save rdillmanCN/051b5376561c0b2afc468725c7bb16c9 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