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.
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.
Share Improve this question edited May 23, 2017 at 12:02 CommunityBot 11 silver badge asked Mar 18, 2014 at 13:15 ZeebaZeeba 1,1221 gold badge12 silver badges16 bronze badges 3 |3 Answers
Reset to default 16Assuming 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);
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);
50000
is50 seconds
. Use5000
instead. – hsz Commented Mar 18, 2014 at 13:16redirect
? Your code makes no sense in its current state. – epascarello Commented Mar 18, 2014 at 13:18redirect
should be a function (or a string, but that's discouraged). It appears to be a DOM element. I'd suggest you read the documentation again. – Frédéric Hamidi Commented Mar 18, 2014 at 13:18