最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript setTimeout 5 seconds - Stack Overflow

programmeradmin4浏览0评论

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
  • 50000 is 50 seconds. Use 5000 instead. – hsz Commented Mar 18, 2014 at 13:16
  • 1 What is redirect? Your code makes no sense in its current state. – epascarello Commented Mar 18, 2014 at 13:18
  • 1 redirect 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
Add a comment  | 

3 Answers 3

Reset to default 16

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);
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);
发布评论

评论列表(0)

  1. 暂无评论