Guest User

Untitled

a guest
Jan 4th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. /**
  2. * Checks for an elements existence within a RAF within a promise.
  3. *
  4. * @param {string} selector - The element you wish to find. Defaults to 'body'.
  5. * @param {string} target - The parent element to search within. Defaults to document.
  6. * @return {Promise} Resolves when the element exists.
  7. */
  8. function elementReady(selector, target) {
  9. var options = {
  10. target: target || document,
  11. selector: selector || 'body'
  12. }
  13. return new Promise(function promise(resolve) {
  14. (function check() {
  15. var el = options.target.querySelector(options.selector);
  16. if (el) {
  17. resolve(el);
  18. }
  19. requestAnimationFrame(check);
  20. })();
  21. });
  22. }
Add Comment
Please, Sign In to add comment