Created
November 3, 2017 11:09
-
-
Save ivoputzer/ee704793f78f3e2a961ab8d046464ad2 to your computer and use it in GitHub Desktop.
different dom ready implementations in vanilla javascript
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
function ready (fn) { | |
if (typeof fn !== 'function') return | |
if (document.readyState === 'complete') return fn() | |
document.addEventListener('DOMContentLoaded', fn, false) | |
} |
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
ready(function domIsReady() { | |
console.info('dom is ready now') | |
}) |
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
function ready (fn, {requestAnimationFrame} = window) { | |
if (document.body) return fn() | |
requestAnimationFrame(ready.bind(null, fn, {requestAnimationFrame})) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment