使用for循环迭代函数x次

4
$('.ip_button').click(function(){
   var buttonValue, slideTimes;
   buttonValue = $(this).html();
   slideTimes = parseInt(buttonValue) - 1;
   for (var i = 0 ; i < slideTimes; i++) { 
          slider.gallery_next(); 
       }             
});

你好,我正在尝试使用一个函数来滑动多张图片,该函数确定按钮的值-1并运行相应次数的滑块。不幸的是,无论按钮中的值是什么,该函数只执行一次。目前按钮中的值为:

<button class="ip_button">5</button>

gallery_next()函数是封装的,代码如下:

this.gallery_next = function() {
    if (galleryRotating) { return; }
    galleryRotating = true;
    var elem = $(galleryId + " > li").first();
    $(galleryId).append(elem.clone());
    $(galleryId).animate({"left" : -imgWidth}, 650, function() {
        elem.remove();
        galleryRotating = false;
        $(galleryId).css({"left" : 0});
    });
1个回答

3
animate函数不会在for循环再次调用该函数之前结束,因此galleryRotation仍然为真,并且该函数将什么也不做。您需要另一种方法:设置一个定时器,在大约700ms后调用函数,或者更好的方法是在通过animate传递的回调中再次调用旋转函数。

它实际上被放入了队列中:http://api.jquery.com/animate/ 在你的问题中,你检查了动画效果是否已经激活,因此动画没有被排队。在jsfiddle示例中,您没有进行检查,因此动画被排队。 - Aneri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接