RouteMapper
RouteMapper

Reputation: 2560

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.

Upvotes: 4

Views: 2022

Answers (3)

Blender
Blender

Reputation: 298246

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.

Upvotes: 9

Paul
Paul

Reputation: 141827

They are synchronous, if there is a reason for you to want them to be asynchronous (Wait until the current execution path finishes, and then empty the queue sometime), you can do:

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

Upvotes: 1

Jeff
Jeff

Reputation: 4146

No, they are chainable methods so they'll finish before returning the original jQuery object.

Upvotes: 2

Related Questions