2

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?

7
  • 3
    I would first try to fix the underlying problem that has you using that event. Commented Feb 5, 2014 at 21:38
  • ?!? Why do you think there is a problem? Commented Feb 5, 2014 at 21:39
  • i've never seen that event used in a place where it wouldn't be better to instead use event delegation or similar methods such as pub/sub. Commented Feb 5, 2014 at 21:42
  • @OleAlbers you should trigger an event when using appending methods instead of a generic event like that. Commented Feb 5, 2014 at 21:42
  • 2
    Per the MDN on Mutation Events: This feature has been removed from the Web. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time. (developer.mozilla.org/en-US/docs/Web/Guide/Events/…) Commented Feb 5, 2014 at 21:42

1 Answer 1

7

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);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.