简体   繁体   English

如何在.each迭代(jquery)上添加.delay到.click

[英]How to add .delay to .click on .each iteration (jquery)

So, I want to put delay on this JavaScript code. 所以,我想延迟这个JavaScript代码。

$(function(){ 
     $('.clickThis').each(function(){ 
         $(this).click();
     }); 
});

I tried this 我试过这个

$(function(){ 
     $('.clickThis').each(function(){
         $(this).click().delay(5000); 
     }); 
});

above script doesnt work . 上面的脚本不起作用。

Is there any alternative? 还有其他选择吗?

I've tried Google it but I still couldn't figure it out, because I have little knowledge in JavaScript. 我已经尝试了谷歌,但我仍然无法弄清楚,因为我对JavaScript知之甚少。

This will do it: 这样做:

$(function(){ 
    $('.clickThis').each(function(i, that){ 
        setTimeout(function(){
            $(that).click();
        }, 5000*i );
    }); 
});

Here's a version using a recursive setTimeout loop. 这是一个使用递归setTimeout循环的版本。

$(function() {
    var click = $('.clickThis').toArray();

    (function next() {
        $(click.shift()).click();   // take (and click) the first entry
        if (click.length) {         // and if there's more, do it again (later)
            setTimeout(next, 5000);
        }
    })();
});

The advantage of this pattern over setTimeout(..., 5000 * i) or a setInterval call is that only a single timer event is ever queued at once. 此模式优于setTimeout(..., 5000 * i)setInterval调用的优点是,只有一个计时器事件一次排队。

In general, repeated calls to setTimeout are better than a single call to setInterval for a few reasons: 通常,重复调用setTimeout比调用setInterval要好一些,原因如下:

  1. setInterval calls can queue up multiple events even if the browser isn't active, which then all fire as quickly as possibly when the browser becomes active again. 即使浏览器未处于活动状态, setInterval调用也可以排队多个事件,然后当浏览器再次激活时,所有事件都会尽可能快地触发。 Calling setTimeout recursively guarantees that the minimum time interval between events is honoured. 以递归方式调用setTimeout确保事件之间的最小时间间隔得到遵守。
  2. With setInterval you have to remember the timer handle so you can clear it 使用setInterval您必须记住计时器句柄,以便清除它

Try to use this: 试着用这个:

$(function () {
    var items=$('.clickThis');
    var length=items.length;
    var i=0;
    var clickInterval=setInterval(function(){
        items.eq(i).click();
        i++;
        if(i==length)
            clearInterval(clickInterval);
    }, 5000);
});

您需要编写一个异步的setTimeout循环,以获取更多信息http://www.erichynds.com/javascript/a-recursive-settimeout-pattern/

var $clickthis=$(".clickthis");
var i= -1;
var delayed = setInterval(function(){
 if (++i < $clickthis.length) $clickthis.eq(i).trigger("click");
 else clearInterval(delayed);
}, 5000);

I am not sure but I think that setTimeout function should do the trick. 我不确定,但我认为setTimeout函数应该可以解决问题。 See here https://developer.mozilla.org/en-US/docs/DOM/window.setTimeout 请参见https://developer.mozilla.org/en-US/docs/DOM/window.setTimeout

Try 尝试

$(function(){ 
     $('.clickThis').each(function(_,i){ 
        var me=$(this);
        setTimeout(function(){me.click()},5000*i);
     ); 
});

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

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