How to Delay The Execution of JavaScript without a plugin

Manuel campos

Delaying the execution of a piece of code means that it will not run immediately when the program is executed, but will be scheduled to run at a later time.

The first time I heard about the execution of JavaScript was when I was playing with Flying Scripts.

Then WPRocket, Perfmatters, and other plugins added similar features to their plugins.

If you don’t want to use a plugin to delay the execution of JavaScript, you can use the following code taken from the Flying Scripts Plugin.

Code to Delay JavaScript

This is the code that you have to insert into your website footer or header to delay internal and external scripts.

This is the unminified version of the script:

    const loadScriptsTimer = setTimeout(loadScripts, 5 * 1000);
    const userInteractionEvents = ["mouseover", "keydown", "touchstart", "touchmove", "wheel"];
    userInteractionEvents.forEach(function (event) {
        window.addEventListener(event, triggerScriptLoader, { passive: !0 });
    function triggerScriptLoader() {
        userInteractionEvents.forEach(function (event) {
            window.removeEventListener(event, triggerScriptLoader, { passive: !0 });
    function loadScripts() {
        document.querySelectorAll("script[data-type='lazy']").forEach(function (elem) {
            elem.setAttribute("src", elem.getAttribute("data-src"));

This is the minified version of the script:

<script> const loadScriptsTimer=setTimeout(loadScripts,5*1000);const userInteractionEvents=["mouseover","keydown","touchstart","touchmove","wheel"];userInteractionEvents.forEach(function(event){window.addEventListener(event,triggerScriptLoader,{passive:!0})});function triggerScriptLoader(){loadScripts();clearTimeout(loadScriptsTimer);userInteractionEvents.forEach(function(event){window.removeEventListener(event,triggerScriptLoader,{passive:!0})})}
function loadScripts(){document.querySelectorAll("script[data-type='lazy']").forEach(function(elem){elem.setAttribute("src",elem.getAttribute("data-src"))})}</script>


These are some examples that show how to wrap the scripts.

If you use Optad360, this is how you delay both scripts:

<script async data-type="lazy" data-src="//"></script>

<script async data-type="lazy" data-src="//"></script></head>

If you want to delay a theme script, you have to dequeue the script and let the Flying Scripts code load after the time you specified.

<script data-type="lazy" data-src=""></script>

More Code Snippets

You might want to check some of these posts before you leave

  1. Add Reusable Blocks to Admin Menu
  2. How to Stop WordPress From Guessing URLs
  3. GeneratePress Hooks: Simple Guide
  4. Removing and Redirecting Query Strings
  5. How To Remove the Category From the WordPress URL
  6. How to Limit WordPress Post Revisions
  7. How to Disable Heartbeat in WordPress
Wanna buy me a Coffee?
About me
I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.
Care for Site Speed?
I think you can’t go wrong with Cloudways, CloudFlare, and Perfmatters.
Perfmatters Cloudways

Resources to host, secure, speed up, rank, and monetize your WordPress sites.