const debounce = (func, delay) => {
let timeoutId = null;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(func.bind(null, ...args), delay);
};
}
const onScroll = (e) => {
const { clientHeight, scrollHeight, scrollTop } = e.target.scrollingElement;
if (scrollTop + clientHeight >= scrollHeight) {
loadMore();
}
};
document.addEventListener("scroll", debounce(onScroll, 300));