简体   繁体   English

jQuery-延迟连续遍历

[英]jQuery - traversing continuously with a delay

I have a bunch of tabs that activate different pages of content and I'd like them to loop automatically until the user clicks on one of them. 我有一堆选项卡可激活内容的不同页面,我希望它们自动循环播放,直到用户单击其中的一个。

$("#projects_list ul li a").each(function() {
 activatetab($(this));
});

So I want activatetab($(this)) to be executed every say 5000ms and once the final tab is activated it should start all over again. 因此,我想每说5000毫秒执行一次activatetab($(this)) ,一旦激活了最后一个选项卡,它应该重新开始。 Can it be done this way? 可以这样吗? I can probably make a function like activate_next_tab() and then call it in setInterval() while tracking which tab was last activated and then finding the next one... but it's a bother. 我可能可以制作一个像activate_next_tab()这样的函数,然后在setInterval()调用它,同时跟踪上一个激活的选项卡,然后找到下一个选项卡...但这很麻烦。 I want to do it this way. 我想这样做。

Thanks 谢谢

Something like: 就像是:

var as = $("#projects_list ul li a"), i = 0, l = as.length;
setInterval(function(){
  activatetab($(as[i++ % l]))
}, 5000)

Some explanations: 一些解释:

  • as is a jQuery object with the list of all A tags as与所有列表中选择一个jQuery对象A标签
  • i is the counter i是柜台
  • l is the number of A tags lA标签的数量
  • the setInterval will repeat the function every 5000ms setInterval每隔5000ms重复一次该函数
  • i++ will increment the counter at every pass, and % l will insure that you never go over the number of A and "rotate" i++将在每次通过时增加计数器, % l将确保您永远不会超过A的数目并“旋转”

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

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