Last active
June 13, 2018 06:47
-
-
Save Antoinebr/e6e20213d80ec104b64e2b4348c3ce26 to your computer and use it in GitHub Desktop.
Tell inlines scripts to wait, that a function is defined https://goo.gl/DF6fWn
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
<!-- Put this function inline on the header --> | |
<script> | |
function whenAvailable(name, callback) { | |
var interval = 10; // ms | |
window.setTimeout(function() { | |
if (window[name]) { | |
callback(window[name]); | |
} else { | |
window.setTimeout(arguments.callee, interval); | |
} | |
}, interval); | |
} | |
</script> | |
<!-- Wrap your inline script into this function, and execute your stuff in the callback --> | |
<script> | |
whenAvailable('myFunction', function(t) { | |
// | |
// Your code here | |
// | |
// When myFunction() will be available this code will be executed | |
// | |
}); | |
</script> | |
<!-- load all your script with defer --> | |
<script src="http://example.com/js/app.js" defer="defer" ></script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See the live demo here : http://codepen.io/antoinebr/pen/gWKXdw