LeoLopesWeb
3/12/2019 - 6:57 PM

Delay in loading external JavaScript file

setTimeout(load_js, 4000); // Delay 4 seconds in loading function
function load_js()
{
    // Get the head tag
    var head_ID = document.getElementsByTagName("head")[0]; 
    // Create script element       
    var script_element = document.createElement('script');
    // Set the script type to JavaScript
    script_element.type = 'text/javascript';
    // External JS file
    script_element.src = 'http:/www.domain.com/script.js';
    head_ID.appendChild(script_element);
}

or

setTimeout(function() {
    // Get the head tag
    var head_ID = document.getElementsByTagName("head")[0]; 
    // Create script element       
    var script_element = document.createElement('script');
    // Set the script type to JavaScript
    script_element.type = 'text/javascript';
    // External JS file
    script_element.src = 'http:/www.domain.com/script.js';
    head_ID.appendChild(script_element);
}, 4000);