I have a situation where I need to load more products from an API when the user scrolls to the middle of the page. I have successfully implemented the code for this feature, but I am facing two issues. Firstly, the function gets called multiple times when I scroll to the end of the products, which is the expected behavior. How can I ensure that the function is called only ONCE when reaching the end of the products? Secondly, I need to determine the direction of the scroll event - specifically, I want to trigger the API request only when the user scrolls from top to bottom and reaches the end of the products.
window.addEventListener('scroll', () => {
if(this.isInViewport){
//api request
}
})
isInViewport(){
const bounding = this.$refs.button.getBoundingClientRect();
return (
bounding.top >= 0 &&
bounding.left >= 0 &&
bounding.bottom <= (window.innerHeight ||
document.documentElement.clientHeight) &&
bounding.right <= (window.innerWidth ||
document.documentElement.clientWidth));
}
My expectation is to make the API request just once per end of the products