Ole Albers
Ole Albers

Reputation: 9285

disable dom changed event in Javascript?

I use

document.addEventListener("DOMSubtreeModified", function() {
    ...
});

to act whenever the DOM of the document is changed.

The problem is: In that function I modify the DOM myself but I do not want this change to call the DOMSubtreeModified event (causing my code to be run over and over again).

Any chance to prevent that?

Upvotes: 2

Views: 1794

Answers (1)

Mehran Hatami
Mehran Hatami

Reputation: 12961

I have done the same once using this solution:

function modifyDOM(obj) {
    obj._muteTrigger = true;

    // HERE do your actual dom change

    obj._muteTrigger = false;
}

document.addEventListener("DOMSubtreeModified", function() {
    if(this._muteTrigger) return;
    
    //the other codes

    modifyDOM(this);
});

Upvotes: 7

Related Questions