Question

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.

Was it helpful?

Solution

Your code is a total mess. Assuming you mean to dynamically load JS resource, that'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);

OTHER TIPS

var myFunction = function(){
    // your function stuff in here.
};
setTimeout(myFunction, 5000);

In this case, the function is available separately from the setTimeout as well.

You have to create a loop function that does what you want. Like this:

(function myloop (i){

        setTimeout(function(){
            //HERE IS YOUR CODE DO WHAT YOU WANT
        },5000)
    })(i);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top