Skip to content

Commit

Permalink
fix: unobserve the observer as soon as it intersects
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyowen committed Apr 30, 2023
1 parent 9960b3d commit 4b2434f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions client/src/components/landing.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Landing = () => {
security: false,
cloud: false,
github: false,
support: false
support: false,
})

const handleChange = (a, b) => setProperties({ ...properties, [a]: b })
Expand Down Expand Up @@ -66,11 +66,19 @@ const Landing = () => {
updateValue()
})
}

if(stars && stars.getAttribute('data-stars')) {
if (!window.IntersectionObserver) countAnimation()
else new IntersectionObserver(a => {
if(a[0].isIntersecting === true) countAnimation()
}, { threshold: [1] }).observe(document.getElementById('counter'))
else {
const observer = new IntersectionObserver(async a => {
// Unobserve the element as soon as the threshold is reached
if(a[0].isIntersecting === true) {
countAnimation();
observer.unobserve(document.getElementById('counter'))
}
}, { threshold: [1] })
observer.observe(document.getElementById('counter'));
}
}
}, [data.stars])

Expand Down

0 comments on commit 4b2434f

Please sign in to comment.