Juan R Labrada

Note Details
OVERLINE
How to execute a function when an element is removed from a html

Use case, we want to know how much time an element has been visible. For example, an ad before refresh.

function onRemove(element, onDetachCallback) {
    const observer = new MutationObserver(function () {
        function isDetached(el) {
            if (el.parentNode === document) {
                return false;
            } else if (el.parentNode === null) {
                return true;
            } else {
                return isDetached(el.parentNode);
            }
        }

        if (isDetached(element)) {
            observer.disconnect();
            onDetachCallback();
        }
    })

    observer.observe(document, {
         childList: true,
         subtree: true
    });
}
Rate this note
Comment
Comments
Back to Notes