I am currently wrapping the IntersectionObserver API into a Svelte action. Part of this requires cleanup in a destroy() function, below is what I have.
function observe(node: HTMLElement, key: string) {
const observer = new IntersectionObserver(nodes => {
locations[key] = nodes[0]!.isIntersecting;
locations = locations;
});
observer.observe(node);
return {
// this is the focus of my question
destroy() {
observer.unobserve(node);
}
}
}
Is there anything else I need to do to destroy the IntersectionObserver?
According to the observe() docs:
To stop observing the element, call
IntersectionObserver.unobserve().
So I think there's nothing more to be done.
Another alternative is to use observer.disconnect() to stop watching all of its target elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With