Possible Duplicate:
Continue Execution Only After .each() Completes
This question is actually a continuation from this discussion. How can we wait each()
to finish its execution given that there is $.get()
inside its callback function?
Working example can be found 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>