Friday, June 23, 2017

Delaying a jquery script until everything else has loaded

Multiple $(document).ready() will fire in order top down on the page. The last $(document).ready()will fire last on the page. Inside the last $(document).ready(), you can trigger a new custom event to fire after all the others..
Wrap your code in an event handler for the new custom event.


<html>
<head>
<script>
$(document).on("my-event-afterLastDocumentReady", function () {
// Fires LAST
});
$(document).ready(function() {
// Fires FIRST
});
$(document).ready(function() {
// Fires SECOND
});
$(document).ready(function() {
// Fires THIRD
});
</script>
<body>
... other code, scripts, etc....
</body>
</html>

<script>
$(document).ready(function() {
// Fires FOURTH
// This event will fire after all the other $(document).ready() functions have completed.
// Usefull when your script is at the top of the page, but you need it run last
$(document).trigger("my-event-afterLastDocumentReady");
});
</script>

1 comment:

Alex said...

If you want to bring your talent to the forefront, and flaunt it to the others, you need a well functioning website. That is precisely why you need portfolio website themes .