简体   繁体   English

一定时间后运行Javascript函数

[英]Running a Javascript function after a certain time

I am new to programming and javascript. 我是编程和JavaScript的新手。

What I want to do : Javascript function running on pageload, in this case showVideo(). 我想做什么:在页面加载上运行的Javascript函数,在这种情况下为showVideo()。 I want to run this function for a say 10 seconds and then move to the next function. 我想运行此功能10秒钟,然后再转到下一个功能。

function(){
     dostuff();
  // dostuff for 10 seconds
  // now stop dostuff
  // donewstuff();
}  

    <div class="wrapper">
        <div class="screen" id="screen-1" data-video="vid/river.mp4">
            <img src="img/bird.jpg" class="big-image" />
        </div>
        <div class="screen" id="screen-2" data-video="vid/sim.mp4">
            <img src="img/spider.jpg" class="big-image" />
        </div>
    </div>
        </div>

<script>
        $(function(){
            var 
                BV,
                videoPlayer,
                BV = new $.BigVideo();
                BV.init();
                showVideo();
                BV.getPlayer();



            function showVideo() {
                BV.show($('#screen-1').attr('data-video'),{ambient:true});
                $('#screen-1').find('.big-image').transit({'opacity':0},500)
                setTimeout(function(){showVideo2},40000);
            }
            function showVideo2() {
                BV.show($('#screen-2').attr('data-video'),{ambient:true});
                $('#screen-2').find('.big-image').transit({'opacity':0},500)
            }

I tried : 我试过了 :

setTimeout(function(){showVideo2},40000) 

but it did not work. 但它没有用。 Any ideas? 有任何想法吗?

You didn't actually call the function. 您实际上并未调用该函数。 Try this: 尝试这个:

setTimeout(function() {
    showVideo2();
}, 40000);

Note the () in showVideo2() . 注意()showVideo2()

You can use setTimeout() 您可以使用setTimeout()

var stopped = false;
setTimeout(function() {
  stopped = true;
}, 10000);

while (!stopped) {
  // Do stuff here.
}

My guess is that you would like to invoke showVideo and then 40s later invoke showVideo2. 我的猜测是,您想调用showVideo,然后在40年代以后调用showVideo2。 What you have is close, however you are not invoking showVideo2 in your timeout. 所剩无几,但是您没有在超时中调用showVideo2。

You have two solutions that would fix it: 您有两种解决方案可以解决此问题:

Change 更改

        function showVideo() {
            BV.show($('#screen-1').attr('data-video'),{ambient:true});
            $('#screen-1').find('.big-image').transit({'opacity':0},500)
            setTimeout(function(){showVideo2},40000);
        }

To either: 要:

        function showVideo() {
            BV.show($('#screen-1').attr('data-video'),{ambient:true});
            $('#screen-1').find('.big-image').transit({'opacity':0},500)
            setTimeout(showVideo2,40000);
        }

or: 要么:

        function showVideo() {
            BV.show($('#screen-1').attr('data-video'),{ambient:true});
            $('#screen-1').find('.big-image').transit({'opacity':0},500)
            setTimeout(function(){showVideo2(); },40000);
        }

Without testing this should work. 未经测试,这应该工作。 Please comment if you have any questions. 如有任何疑问,请发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM