|
| 1 | +# Episode 15 : Callbacks and Event Listeners |
| 2 | + |
| 3 | +**Callback Function :** Functions are first class citizens (see prev lecture) ie. take a fun A and pass it to another fun B. Here, A is a callback function |
| 4 | +- JS is a synchronous and singlethreaded language. But due to callbacks, we can do async things in JS. |
| 5 | + |
| 6 | +> setTimeout(function () {}, 1000) -> here the anony function is a callback function as it is passed to setT and called sometime later in code after certain time (here 1000ms). |
| 7 | +- This is how we do async things. JS is a synch language, but it doesn't wait 1 sec for setT to finish before going to code below it. It stores the function, attaches timer |
| 8 | +and goes down the code. |
| 9 | + |
| 10 | +``` |
| 11 | +setTimeout(function () { |
| 12 | + console.log("timer"); |
| 13 | + }, 5000); |
| 14 | + |
| 15 | +function x(y) { |
| 16 | + console.log("x"); |
| 17 | + y(); |
| 18 | +} |
| 19 | +
|
| 20 | +x(function y() { |
| 21 | + console.log("y"); |
| 22 | +}); |
| 23 | +``` |
| 24 | + |
| 25 | +> x |
| 26 | +
|
| 27 | +> y |
| 28 | +
|
| 29 | +> timer |
| 30 | +
|
| 31 | +- In the call stack, first x and y are present. After completion, they go away and stack is empty. Then after 5 seconds(from beginning) anonymous suddenly pops up in stack ie. setTimeout |
| 32 | +- All 3 functions are executed through call stack. If any operation blocks the call stack, its called **blocking the main thread** |
| 33 | +- Say if x() takes 30 sec to run, then JS has to wait for it to finish as it has only 1 call stack/1 main thread. *Never block main thread*. |
| 34 | +- **Always use async for functions that take time eg. setTimeout** |
| 35 | + |
| 36 | +**Event Listener** |
| 37 | + |
| 38 | +- When we create a button in HTML and attack a clickListener in JS : |
| 39 | +``` |
| 40 | +in index.html |
| 41 | +
|
| 42 | +<button id="clickMe">Click Me!</button> |
| 43 | +
|
| 44 | +in index.js |
| 45 | +
|
| 46 | +document.getElementById("clickMe").addEventListener("click", function xyz(){ //when event click occurs, this callback function is called into callstack |
| 47 | + console.log("Button clicked"); |
| 48 | +}); |
| 49 | +``` |
| 50 | + Suppose we want to increase count by 1 each time button clicked. |
| 51 | + - Use global variable (not good as anyone can change it) |
| 52 | + |
| 53 | + ``` |
| 54 | + let count = 0; |
| 55 | + document.getElementById("clickMe").addEventListener("click", function xyz(){ |
| 56 | + console.log("Button clicked", ++count); |
| 57 | +}); |
| 58 | + ``` |
| 59 | +- Use closures for data abstraction |
| 60 | + |
| 61 | + ``` |
| 62 | + function attachEventList() { //creating new fun for closure |
| 63 | + let count = 0; |
| 64 | + document.getElementById("clickMe").addEventListener("click", function xyz(){ |
| 65 | + console.log("Button clicked", ++count); //now callback fun forms closure with outer scope(count) |
| 66 | +}); |
| 67 | +} |
| 68 | +attachEventList(); |
| 69 | +``` |
| 70 | + |
| 71 | +#### Garbage Collection and removeEventListeners |
| 72 | +- Event listeners are heavy as they form closures. So even when call stack is empty, EventListener won't free up memory allocated to *count* as it doesn't know |
| 73 | +when it may need *count* again. |
| 74 | +- **So we remove event listeners when we don't need them (garbage collected)** |
| 75 | +- onClick, onHover, onScroll all in a page can slow it down heavily. |
0 commit comments