可能重複:
Continue Execution Only After .each() Completes等待。每()來完成,考慮到。每()的Ajax調用
這個問題實際上是從this discussion的延續。考慮到其回調函數中有$.get()
,我們如何等待each()
完成其執行?
可以找到工作實例here。
/* JavaScript/jQuery. */
<script>
function prepareLayer($n) {
$.get('./a.html', function(data) {
/* a.html contains: <a href="javascript:void(0);">Click me!</a> */
$n.html(data);
});
}
function postPreparation() {
$('.element a').click(function() {
alert('Ouch... you just clicked me!');
});
}
$(function() {
$('.element').each(function() {
prepareLayer($(this));
});
postPreparation();
});
</script>
<!-- HTML -->
<div class="element"></div>
<div class="element"></div>
請更新喲你之前的問題,而不是隻是輕微的更新重新發布。 Alnitaks的答案是allready,包括異步請求的解決方案。 – Yoshi 2012-01-28 10:15:20
等待@Yoshi,那個問題中接受的答案並不能解決如何解決這個問題。它只證實了這裏描述的事實/問題。 – moey 2012-01-28 10:20:50
確定它確實如果你看看[.when]的文檔(http://api.jquery.com/jQuery.when/) – Yoshi 2012-01-28 10:22:52