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 });
.item2you should call obsTargetEl.disconnect() before changeItem(); and also remove mList.forEach because you're not actually using the enumeration i.e. just leave theifcheck.disconnect()but seems not working" - you need to show us what exactly you tried, otherwise we can't help find or fix the problem!.item2only withintargetEl? And why are you using jQuery for this?