Do jQuery empty () and remove () functions asynchronously?

jQuery.fn.empty() and remove() functions executed asynchronously? I cannot find the answer to this question in any jQuery documentation.

+4
source share
3 answers

They are both synchronous. You can see the source for the actual implementation:

 remove: function( selector, keepData ) { var elem, elems = selector ? jQuery.filter( selector, this ) : this, i = 0; for ( ; (elem = elems[i]) != null; i++ ) { if ( !keepData && elem.nodeType === 1 ) { jQuery.cleanData( getAll( elem ) ); } if ( elem.parentNode ) { if ( keepData && jQuery.contains( elem.ownerDocument, elem ) ) { setGlobalEval( getAll( elem, "script" ) ); } elem.parentNode.removeChild( elem ); } } return this; }, empty: function() { var elem, i = 0; for ( ; (elem = this[i]) != null; i++ ) { if ( elem.nodeType === 1 ) { // Prevent memory leaks jQuery.cleanData( getAll( elem, false ) ); // Remove any remaining nodes elem.textContent = ""; } } return this; }, 

You can ignore the keepData and cleanData , so all you have to do is loop and call your own DOM method or modify a property of the DOM object. They are both synchronous.

+9
source

No, they are chain methods, so they will be finished before returning the original jQuery object.

+2
source

They are synchronous, if there is a reason why you want them to be asynchronous (wait for the current execution path to finish and then release the queue), you can do:

 // Empty asynchronously setTimeout(function(){ $('...').empty(); }, 0); 
+1
source

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


All Articles