我只是想在 jQuery Each 循环的每次迭代之后添加一个暂停,我似乎无法弄清楚。
$(".item").each(function(i){
var el = this;
var timer = setTimeout(function(){
$(el).trigger("click");
}, 500);
});
这并不是每 1/2 秒触发一次,而是一次触发所有项目的点击。
我想要这样的东西(伪代码):
$(".item").each(function(i){
$(this).trigger("click");
wait(500); // wait a half second before the next iteration
});
这有什么工作方法吗?
你真的不能这样做$.each
。您可以使用setTimeout
并保留对您当前所在索引的引用:
function process(elements, cb, timeout) {
var i = 0;
var l = elements.length;
(function fn() {
cb.call(elements[i++]);
if (i < l) {
setTimeout(fn, timeout);
}
}());
}
process($('.item'), function() {
$(this).click();
}, 500);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)