Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Do jQuery.fn.empty() and remove() functions execute asynchronously? I can't find an answer to this question anywhere in the jQuery documentation.

like image 604
RouteMapper Avatar asked Dec 15 '22 09:12

RouteMapper


1 Answers

They're both synchronous. You can look at 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 stuff, so all you're left with is a loop and a call to a native DOM method or a DOM object property modification. Those are both synchronous.

like image 187
Blender Avatar answered Apr 05 '23 23:04

Blender