Created
February 4, 2024 12:36
-
-
Save herdianf/c1574a875429ef9cc77bd928eac32b0e to your computer and use it in GitHub Desktop.
dom ready
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
var domReady = (function() { | |
var readyCallbacks = []; | |
function whenReady(callback) { | |
setTimeout(function() { | |
callback.call(document) | |
}); | |
} | |
function completed() { | |
document.removeEventListener('DOMContentLoaded', completed); | |
window.removeEventListener('load', completed); | |
while (readyCallbacks.length) { | |
let cb = readyCallbacks.shift(); | |
whenReady(cb); | |
} | |
} | |
return function(callback) { | |
if (document.readyState === 'loading') { | |
if (readyCallbacks.length === 0) { | |
document.addEventListener('DOMContentLoaded', completed); | |
window.addEventListener('load', completed); | |
} | |
readyCallbacks.push(callback) | |
} else { | |
whenReady(callback); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment