Skip to content

Instantly share code, notes, and snippets.

@Antoinebr
Last active June 13, 2018 06:47
Show Gist options
  • Save Antoinebr/e6e20213d80ec104b64e2b4348c3ce26 to your computer and use it in GitHub Desktop.
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
<!-- 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>
@Antoinebr
Copy link
Author

See the live demo here : http://codepen.io/antoinebr/pen/gWKXdw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment