0

First time using mutation observer, tried to add disconnect() but seems not working. Need some advice. Thanks.

const targetEl = document.querySelector('.swiper-container'); const cbMutate = (mList) => { mList.forEach( (rec) => { if (jQuery('.item2').length === 1) { obsTargetEl.disconnect(); changeItem(); } } ); } const obsTargetEl = new MutationObserver(cbMutate); obsTargetEl.observe(targetEl, { attributes: true, childList: true, subtree: true }); 
7
  • 1
    Assuming you want to stop after finding .item2 you should call obsTargetEl.disconnect() before changeItem(); and also remove mList.forEach because you're not actually using the enumeration i.e. just leave the if check. Commented May 31, 2024 at 9:39
  • 1
    "tried to add disconnect() but seems not working" - you need to show us what exactly you tried, otherwise we can't help find or fix the problem! Commented May 31, 2024 at 16:30
  • Shouldn't you search for .item2 only within targetEl? And why are you using jQuery for this? Commented May 31, 2024 at 16:31
  • @Bergi I have added disconnect() above but changeItem() is not activated with it Commented Jun 13, 2024 at 8:04
  • @woxxom thanks, I have tried to call obsTargetEl.disconnect() before changeItem(); but changeItem() does not run after Commented Jun 13, 2024 at 8:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.