Skip to content

Commit b333da0

Browse files
chore: implement throttle
1 parent 54ef9b6 commit b333da0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/performance.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ If we have not throttled the function which fire on every button click we would
5858

5959
## Implement Throttle
6060

61+
* We are passing a function(fn) and a limit (limit) into the throttle function
62+
* isThrottled is the variable which we use to keep track of if throttle period has passed or not
63+
64+
THe first call to our function fn will execute and then we will set the isThrottled to true, also the isThrottled will become false again when our limit period has passed. Now, if throttle is true and limit period has not passed, our function fn during this period will not fire. Once it is passed, the next invocation will fire and the process will repaeat.
65+
66+
```jsx
67+
function throttle(fn, limit) {
68+
let isThrottled;
69+
return function() {
70+
if (!isThrottled) {
71+
fn.apply(this, arguments);
72+
isThrottled = true;
73+
setTimeout(() => {
74+
isThrottled = false
75+
}, limit);
76+
}
77+
}
78+
}
79+
```
80+
6181
## Explain requestAnimationFrame
6282

6383
## Move element left to right by any given distance smoothly

0 commit comments

Comments
 (0)