Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,37 @@
---
title: Handle scroll stop
type: snippet
language: javascript
tags: [browser,event]
author: chalarangelo
cover: flower-pond
dateModified: 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 wait `150` ms until calling the given `callback`.
- Use `clearTimeout()` to clear the timeout if a new `'scroll'` event is fired in under `150` ms.
```js
const onScrollStop = callback => {
let isScrolling;
window.addEventListener(
'scroll',
e => {
clearTimeout(isScrolling);
isScrolling = setTimeout(() => {
callback();
}, 150);
},
false
);
};
```
```js
onScrollStop(() => {
console.log('The user has stopped scrolling');
});
```