-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThrottle.jsx
More file actions
59 lines (46 loc) · 1.68 KB
/
Throttle.jsx
File metadata and controls
59 lines (46 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Throttle.js
import React, { useEffect } from 'react';
/*
🎯 THROTTLE — Definition:
Throttle limits the execution of a function to once every "X" milliseconds, **no matter how many times the event is triggered**.
🧠 Example use case:
If you're listening to a scroll or mousemove event which fires many times per second, throttle ensures the function only runs every X ms.
🚫 Without throttle: fn() gets called 100s of times.
✅ With throttle: fn() gets called once every limit ms.
🔁 HOW IT WORKS:
- Save last time function ran (lastCall)
- If (current time - lastCall >= limit), run the function and update lastCall
- Otherwise, ignore the call
*/
function throttle(fn, limit) {
let lastCall = 0;
return (...args) => {
const now = Date.now(); // current timestamp in ms
if (now - lastCall >= limit) {
lastCall = now; // update last time it ran
fn(...args); // call the function
}
};
}
// 👇 Example Component using Throttle
const Throttle = () => {
useEffect(() => {
const onScroll = () => {
console.log('🔄 Scroll event triggered:', new Date().toLocaleTimeString());
};
// Throttled version: only fires once every 1000ms
// const throttledScroll = throttle(onScroll, 10000);
const throttledScroll = throttle(onScroll, 1000);
window.addEventListener('scroll', throttledScroll);
return () => {
window.removeEventListener('scroll', throttledScroll);
};
}, []);
return (
<div style={{ height: '200vh', padding: '20px' }}>
<h2>🧭 Scroll down to see throttle in action</h2>
<p>Open dev tools console to watch throttled logs appear once per second.</p>
</div>
);
};
export default Throttle;