让函数在正确的时间执行(排队或同时)

时间:2011-05-08 11:45:33

标签: jquery execution-time queuing

我写了一个随机播放一系列div的小脚本 - 这可以按预期(或希望)工作。

我的问题在于实施。我希望divs淡出,被洗牌并再次淡入。我发现函数moveBox()与任何动画同时执行。我已经尝试将它作为回调函数调用动画中的所有元素(fadeOut,delay和fadeIn),但总是具有相同的效果 - div的改组和重新分配在动画期间发生,因此是可见的。 / p>

我有一个解决方案(var ts = timeOut ...),这会在隐藏div时发生shuffle,但我不相信这是最好的解决方案。

我想知道如何控制函数的执行顺序以及它们是应该同时执行还是按顺序执行。 我的代码:

<style>
    .tester{
        float:left;
        width:100px;
        height:100px;
        margin:5px;
        }
    .tester p{text-align:center;
        margin-top:20px;
    }
    .one{background-color:red;}
    .two{background-color:yellow;}
    .three{background-color:teal;}
    .four{background-color:blue;}
    .five{background-color:green;}
    .six{background-color:silver;}
</style>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    jQuery(document).ready(function(){
        var anibase=jQuery(".tester").length;
        jQuery(".tester").fadeOut(1000);
        function moveBox(){
            function shuffle(){
                for (i = 0; i <= (anibase - 2); i++) {
                    mover = Math.floor(Math.random() * (anibase - i));
                    if (mover != 0) {
                        mover = mover + i;
                        jQuery(".tester:eq(" + mover + ")").insertBefore(".tester:eq(" + i + ")");
                    };
                };
            };
            jQuery(".tester").fadeOut(1500).delay(500).fadeIn(1500);
            var ts = setTimeout(shuffle,1500);
            var t=setTimeout(moveBox,5000);
        };
        moveBox();
    });

</script>
<body>
    <div class="tester one">
        <p>box 1</p>
    </div>
    <div class="tester two">
        <p>box 2</p>
    </div>
    <div class="tester three">
        <p>box 3</p>
    </div>
    <div class="tester four">
        <p>box 4</p>
    </div>
    <div class="tester five">
        <p>box 5</p>
    </div>
    <div class="tester six">
        <p>box 6</p>
    </div>
</body>

提前致谢

1 个答案:

答案 0 :(得分:0)

JQuery能够嵌套函数,有效地控制执行顺序。

首先虽然在这种情况下在Javascript中嵌套函数是有效的,但是没有必要,请考虑以下代码:

<script type="text/javascript">

  jQuery(document).ready(function(){
    moveBox();  //call the function that will do the work and then setTimeout
  });



  function moveBox(){
    jQuery(".tester").fadeOut("slow",function(){
      shuffle();
      $(this).fadeIn("slow");
      return;
    });
    var t=setTimeout(moveBox,5000);
  }

  function shuffle(){
    var anibase=jQuery(".tester").length;
    for (i = 0; i <= (anibase - 2); i++) {
      mover = Math.floor(Math.random() * (anibase - i));
      if (mover != 0) {
        mover = mover + i;
        jQuery(".tester:eq(" + mover + ")").insertBefore(".tester:eq(" + i + ")");
      }
    }
  }
</script>

这里要看的重点是:

jQuery(".tester").fadeOut("slow",function(){
  shuffle();
  $(this).fadeIn("slow");
  return;
});

这将指示jQuery

  1. 淡出.tester元素
  2. 淡出完成后执行shuffle()
  3. 然后在
  4. 中淡化元素

    您可以在fadeOut文档中查看更多示例,其中一些示例显示链接事件

    另外,通过将功能定义分开,可以更容易阅读。