852 B
852 B
title, tags, expertise, author, cover, firstSeen, lastUpdated
| title | tags | expertise | author | cover | firstSeen | lastUpdated |
|---|---|---|---|---|---|---|
| Handle scroll stop | browser,event | intermediate | chalarangelo | blog_images/flower-pond.jpg | 2021-01-07T00:31:14+02:00 | 2021-01-07T00:31:14+02:00 |
Runs the callback whenever the user has stopped scrolling.
- Use
EventTarget.addEventListener()to listen for the'scroll'event. - Use
setTimeout()to wait150ms until calling the givencallback. - Use
clearTimeout()to clear the timeout if a new'scroll'event is fired in under150ms.
const onScrollStop = callback => {
let isScrolling;
window.addEventListener(
'scroll',
e => {
clearTimeout(isScrolling);
isScrolling = setTimeout(() => {
callback();
}, 150);
},
false
);
};
onScrollStop(() => {
console.log('The user has stopped scrolling');
});