Skip to content

Commit 5920a48

Browse files
authored
Create throttle.js
1 parent 0304586 commit 5920a48

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

throttle.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
the handler would be invoked atmost once in `limit` seconds
3+
examples: infinite scrolling check for the bottom of the screen etc...
4+
*/
5+
const throttle = (handler, limit) => {
6+
let inThrottle = false;
7+
return function () {
8+
if (!inThrottle) {
9+
handler.apply(this, arguments);
10+
inThrottle = true;
11+
setTimeout(() => inThrottle = false, limit);
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)