Reputation: 1122
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.
Upvotes: 2
Views: 50541
Reputation: 66388
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);
Upvotes: 16
Reputation: 706
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);
Upvotes: -2
Reputation: 374
var myFunction = function(){
// your function stuff in here.
};
setTimeout(myFunction, 5000);
In this case, the function is available separately from the setTimeout as well.
Upvotes: 1