JQuery each () and "on success"?

I use $().each()to scroll some elements. I want to make sure that the action that follows this script fragment is only executed after completion each().

Example:

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

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

It seems to work at this stage, because this is really the main action. But sometimes this fails and it seems that the part each()is still busy, as a result of which the action of the subsequent script will be overwritten by the actions each().

So ... is this possible? Or the code under each function () is always executed after the previous code. I know that, for example, AJAX calls have a “success” function to force the code to execute only when the parent action completes. Is it necessary / possible here? I tried something like the following, but this does not seem to work:

$('something').each(function() {
  // do stuff to items
}, function () {
  // do stuff when the each() part has completed
});
+3
source share
1 answer

eachis not asynchronous, so everything that comes after this should be done afterwards, and there is no support for the callback. However, this will depend on what your “do things for elements” does, because some of them may be asynchronous - for example, animation, slide up / down, etc. - and these methods support callbacks.

+5
source

Source: https://habr.com/ru/post/1714407/


All Articles