Search code examples
javascriptsettimeout

JavaScript setTimeout 5 seconds


Trying to get a setTimeout to load a JS after 5 seconds, I can't seem to get it to work; closest one I can find on the forums is this Problem with setTimeout

What I was trying was this:

<script type="text/javascript">
    function()
    {
        var load = setTimeout(redirect, 5000);
        redirect.src = src="js/load.js";
    }
</script>

JavaScript is not my strongest area.


Solution

  • Assuming you mean to dynamically load JS resource, here's the way to do it.

    First, have this:

    <script type="text/javascript" id="redirect"></script>
    

    And the code:

    var load = setTimeout(function() {
        document.getElementById("redirect").src="js/load.js";
    }, 5000);