-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeBounceTemp.js
More file actions
80 lines (46 loc) · 1.4 KB
/
deBounceTemp.js
File metadata and controls
80 lines (46 loc) · 1.4 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//most useful concept for performance optimization by limiting function calls on different events
//very common frontend interview
//keyup bs input vs change input events
//keyup : fired when key is released after pressed
console.log('hello')
const myInput= document.getElementById('input-event')
myInput.addEventListener('input',(e)=>findSuggestion(e))
//input : fired when key is pressed not released <Most PREFFERED>
//change : fired when the focus is changed from the given object
//keypress :
//this.value vs e.target.value
//use this. when not using arrow function
//________________________________________________________________________________________________
//what is debouncing?
const findSuggestion=(e)=>{
console.log('your suggestion for :' , e.target.value )
}
setTimeout(()=>{
findSuggestion
},1000)
// what are decorator functions
//takes function and return fucntion
function decorator(func){
return function(){
func();
}
}
function hello(){
console.log('hell o')
}
const newfunc=decorator(hello)
newfunc();
//implement debouncing
function deBounce(func,delay){
let timeId;
return function(...args){
if(timeId){
clearTimeout(timeId);
}
timeId=setTimeout(()=>{
},delay)
func.call(this, ...args);
}
}
const decoratedFunc=deBounce(findSuggestion,300)
//understanding debpuncing in depth