代码之家  ›  专栏  ›  技术社区  ›  kush

jQuery:在每个迭代器中使用时间延迟?

  •  4
  • kush  · 技术社区  · 14 年前

    我正在尝试创建一个旋转木马的效果,每3秒自动循环通过每张图片。

        $(".headline_img").each(function(intIndex){
            setTimeout($(this).show(),3000);
        });
    

    我错过什么了吗?

    注意:我用$(document)来调用它。准备好了,你认为这会影响它吗?

    3 回复  |  直到 14 年前
        1
  •  5
  •   Heretic Monkey goproxy.dev    9 年前

    这个 setTimeout 函数接受函数引用或字符串。你的代码调用 show 方法。我不确定这是否有效:

    $(function () {
      var t = 3000, $debug = $('#result');
        $(".headline_img").each(function(){
          var $img = $(this);
            setTimeout($img.show.bind($img), t);
          t += 3000;
        });
    });
    .headline_img { display: none; }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div class="headline_img">One</div>
    <div class="headline_img">Two</div>
    <div class="headline_img">Three</div>

    但值得一试。。。

        2
  •  2
  •   jball    14 年前

    $(".headline_img").each(function(intIndex){
        setTimeout($(this).show(),3000 * (intIndex +1));
    });
    

    重构以使用 queue

        3
  •  0
  •   Leysam Rosario    9 年前

    或者您可以使用jQuery的 delay

    $(".headline_img").each(function(intIndex){
        $(this).delay(3 * (intIndex + 1)).show();
    });