use it delay Javascript loading for some sec you can use dom ready event only instead of the setTimeout. like this ", "dateCreated": "2023-07-27 00:00:00", "upvoteCount": 0, "url": "https://www.arrayoverflow.com/question/how-to-delay-javascript-loading/3613#2306", "author": { "@type": "Person", "name": "jqueryLearner" } } ] } }
Asked 1 years ago
26 Jul 2023
Views 587
ruby-rails

ruby-rails posted

how to delay Javascript Loading ?


i have very low page speed score . where it have lots of JavaScript loading
but if i remove JavaScript it get good speed
so how can i delay Javascript Loading
jqueryLearner

jqueryLearner
answered Jul 27 '23 00:00

use async

<script src="https://www.gggg.ch/heavyloader.js" async ></script>

---------------- or -----------------
use defer


<script  src="https://www.gggg.ch/heavyloader.js"  defer    ></script> 



---------------- or -----------------

  function renderJs(url) {
    var script = document.createElement('script');
    script.src = url;
    document.documentElement.firstChild.appendChild(script);
}
window.onload = function() {
  setTimeout(function(){
    renderJs("https://www.gggg.ch/heavyloader.js");
},2000); 
}


it delay Javascript loading for some sec
you can use dom ready event only instead of the setTimeout.
like this

Post Answer