I am using Intersection Observer API in react to detect an element coming on viewport. But I also want that the element should remain on viewport for minimum 3 seconds and detected after that. How can I do so? Give below is the code I am using.
const callBack = (entries) => {
const [entry] = entries;
if (entry.isIntersecting) {
console.log("intersecting");
}
};
const options = {
root: null,
rootMargin: "0px",
threshold: 0.75,
};
useEffect(() => {
const observer = new IntersectionObserver(callBack, options);
if (cardRef.current) {
observer.observe(cardRef.current);
}
return () => {
if (cardRef.current) {
observer.unobserve(cardRef.current);
}
};
}, []);