Throttling in JavaScript

Defination

English wale

Trottling is a technique in which, no matters how many times user fires the event, the attached function executed only once in a given time interval.

Hindi Wale

Dekho bhai user bar bar button click kar ke jyada payment na kar de 😂😂 .Esliye button click karte hi usko kuch time disable kr dete hai. Jisase ki galti se 1 se jyada bar click na hoye. Yahi apni throttling hai.

Example 1

In This example when you clicked button it delays for 3 second before next call.

0

1const Example1 = () => {
2        const [isTime, setIsTime] = useState(false);
3        const [count, setCount] = useState(0);
4
5        const throttleFun = (call, delay) => {
6          return function () {
7            if (isTime) return;
8            setIsTime(true);
9            call();
10      
11            setTimeout(() => {
12              setIsTime(false)
13            }, delay);
14          };
15        };
16      
17        const runFun = throttleFun(() => {
18            setCount(count + 1);
19        }, 3000);
20      
21        return (
22          <>
23            <button style={isTime ? { background: "red" } : {background:'blue'}} onClick={runFun}>Click</button>
24            <p>{count}</p>
25          </>
26        );
27      };
Hello