Created
February 24, 2020 23:57
-
-
Save besrabasant/331f750bb24eb0694f3157270a331f88 to your computer and use it in GitHub Desktop.
On DOM Ready function
This file contains hidden or 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
var domLoadedPromise = null | |
export default function onDomReady(documentReadyCallback) { | |
if (!domLoadedPromise) { | |
const readyReg = /complete|loaded|interactive/ | |
domLoadedPromise = new Promise(function (resolve) { | |
if (readyReg.test(document.readyState) && document.body) { | |
resolve() | |
} else { | |
document.addEventListener('DOMContentLoaded', function onDomLoaded() { | |
document.removeEventListener('DOMContentLoaded', onDomLoaded, false) | |
resolve() | |
}, false) | |
} | |
}) | |
} | |
if (documentReadyCallback) { | |
domLoadedPromise.then(documentReadyCallback) | |
} | |
return domLoadedPromise | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment