如何循环setTimeOut函数

时间:2015-03-16 06:12:35

标签: javascript jquery

我是关于javascript和jquery的新手,我在这个小提琴上创建了一些函数,它只运行一次,如何循环这个函数?

    $(document).ready(function()  // function
    { 

     $('.box').css({visibility:'hidden'});

      setTimeout( function(){
        $('.box-1').css({visibility:'visible'});  
       },300);
      setTimeout( function(){
        $('.box-2').css({visibility:'visible'}); 
     },600);
     setTimeout( function(){
       $('.box-3').css({visibility:'visible'}); 
    },900);  
    setTimeout( function(){
      $('.box').fadeOut('slow'); 
   },1300);  
})

小提琴链接https://jsfiddle.net/dzh5dgcf/

1 个答案:

答案 0 :(得分:4)

使用setInterval()

$(document).ready(function () {

    function animate() {
        $('.box').css({
            visibility: 'hidden',
            display: ''
        });
        setTimeout(function () {
            $('.box-1').css({
                visibility: 'visible'
            });
        }, 300);
        setTimeout(function () {
            $('.box-2').css({
                visibility: 'visible'
            });
        }, 600);
        setTimeout(function () {
            $('.box-3').css({
                visibility: 'visible'
            });
        }, 900);
        setTimeout(function () {
            $('.box').fadeOut('slow');
        }, 1300);
    }
    animate()
    setInterval(animate, 1800)
});

演示:Fiddle