tags:

views:

110

answers:

1

I'm using $().each() to loop through some items. I want to make sure that the action that follows after this piece of script is only executed when each() has completed.

Example:

$('something').each(function() {
  // do stuff to items
});

// do something to one of these items
$('item').etc

It seems to work at this point, because it's a really basic action. But sometimes it fails and it seems like the each() part is still busy, causing the action of the follow-up script to be overwritten by the each() actions.

So... is this possible? Or is code below the each() function always executed after the previous code. I know that e.g. AJAX calls have a "success" function, to force code to only execute when the parent action is completed. Is this needed/possible here? I tried something like the following, but this doesn't seem to be working:

$('something').each(function() {
  // do stuff to items
}, function () {
  // do stuff when the each() part has completed
});
+4  A: 

each isn't asynchronous, so whatever comes after should execute afterwards and there isn't any support for a callback. However, it will depend what your 'do stuff to items' does, because some of this could be asynchronous - eg animations, slide up/down, etc - and these methods support callbacks.

Daniel Roseman