2013-05-01 1 views
4

각 루프마다 jQuery를 반복 할 때마다 일시 정지를 추가하기 만하면됩니다.jQuery에서 대기/일시 정지/수면 반복 사이의 각 루프

$(".item").each(function(i){ 
    var el = this; 
    var timer = setTimeout(function(){ 
     $(el).trigger("click"); 
    }, 500); 
}); 

이것은 1 초마다 실행되는 것이 아니라 모든 항목을 한 번에 트리거합니다.

나는이 (의사) 같은 것을 원하는이의

$(".item").each(function(i){ 
    $(this).trigger("click"); 
    wait(500); // wait a half second before the next iteration 
}); 

모든 작업 방법을?

답변

2

안녕이 시도하거나 당신은 이미 시도를 A의 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(); 
}); 
3

당신은 정말 $.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); 
+0

+1! –