-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDebouncing.js
More file actions
28 lines (21 loc) · 754 Bytes
/
Debouncing.js
File metadata and controls
28 lines (21 loc) · 754 Bytes
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
//This functionality works when we have search bar and we need to make API Calls.
//This approach is used to restrict the number of API calls
//It doesn't make call on every key stroke but makes call after specific timeout.
let counter = 0;
const getData = () => {
console.log("Fetchiing Data ...", counter++);
}
const debounce = function(fn, d){
let timer;
return function(){
let context = this,
args = arguments;
clearTimeout(timer);
timer = setTimeout(()=>{
getData.apply(context, arguments);
}, d);
}
}
const debouncedFunction = debounce(getData, 300);
debouncedFunction();
//The time between 2 keypress event is more than some set amount of time like say 300 ms.