在迭代之间的jQuery每个循环中等待/暂停/hibernate
我只是想在每次循环jQuery的每次迭代后添加一个暂停,我似乎无法弄明白。
$(".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
真正做到这$.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);
Hiya尝试这个,或者你可以写一个你已经尝试过的SetTimeOUt函数
演示=> http://jsfiddle.net/Yq2SL/2/ you will see different parent of div with class=item
API: http : //api.jquery.com/jQuery.dequeue/&http : //api.jquery.com/jQuery.queue/#jQuery-queue1
您阅读的资料很少:
http://forum.jquery.com/topic/delay-and-click-issue
http://blog.project-sierra.de/archives/1559
希望它有帮助:)
示例代码:
$(".item").delay(500).queue(function(){ $(this).trigger("click"); $(this).dequeue(); });