使用setInterval jquery each() [英] jquery each() with setInterval

查看:70
本文介绍了使用setInterval jquery each()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个填充了各种元素的对象,我希望使用 each()进行迭代,然后对轮到它的元素执行操作。所以:

I have an object filled with various elements that I wish to iterate through using each() and then perform an action on the element whose turn it is. So:

var arts = $("#press-sqs > article");
shuffle(arts);

$(arts).each(function(){
    setInterval(function() {
    // in here perform an action on the current element in 'arts'
    }, 2000);   
}); 

shuffle()是一个基本的洗牌函数)

( shuffle() is a basic shuffle function )

我无法弄清楚的是如何将当前元素作为选择器访问并对其执行操作。 $(this) $(窗口)

What I can't figure out is how to access the current element as a selector and perform an action on it. $(this) is $(window).

最后,我需要该函数在到达 art 结束时再次启动迭代,并继续无限循环。

Finally I would then need the function to start the iteration again once it reaches the end of art and keep on looping ad infinitum.

推荐答案

如果你正在使用 setInterval ,你会得到相同的结果来交换订单:

If you're using setInterval, you'd get identical results swapping the order:

setInterval(function() {
    $(arts).each(function(){
         doSomethingWith(this);
    });   
}, 2000);

我认为你不想要你在这里做的事。我想你想要:

I don't think you want what you think you do here. I reckon you want:

var i = 0;
setInterval(function() {
    var art = arts[i++];
    doSomethingWith(art)
    if(i >= arts.length) i = 0;
}, 2000); 

这篇关于使用setInterval jquery each()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆