Skip to content
Advertisement

How to determine if mutation has been called?

I have this mutation observer function below. The issue is that if the condition is met for “false” that console.log will log many times in the browser. I think the best way to fix this is to add something in here that if the mutation is met don’t run function.

Does adding in mutationObserver.disconnect(); after the else if condition the best way to accomplish this?

JavaScript

Advertisement

Answer

Use .some instead, to iterate over the mutations and see if any of the targets’ classLists contain the class:

JavaScript

Keep in mind that forEach ignores its return value; the return true you currently have doesn’t do anything.

Advertisement