diff options
Diffstat (limited to 'assets/js/utils.js')
-rw-r--r-- | assets/js/utils.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/assets/js/utils.js b/assets/js/utils.js new file mode 100644 index 0000000..d48d708 --- /dev/null +++ b/assets/js/utils.js @@ -0,0 +1,29 @@ +// borrowed from https://stackoverflow.com/questions/9899372/pure-javascript-equivalent-of-jquerys-ready-how-to-call-a-function-when-t +function docReady(fn) { + // see if DOM is already available + if (document.readyState === "complete" || document.readyState === "interactive") { + // call on next available tick + setTimeout(fn, 1); + } else { + document.addEventListener("DOMContentLoaded", fn); + } +} + +function windowLoaded(fn) { + // see if we're already loaded + if (document.readyState === "complete") { + // call on next available tick + setTimeout(fn, 1); + } else { + window.addEventListener("load", fn); + } +} + +function onWindowResize(fn) { + windowLoaded(function () { + window.addEventListener('resize', fn); + setTimeout(fn, 1); + }); +} + +export { docReady, windowLoaded, onWindowResize}; |