Skip to content

Commit 6088804

Browse files
committed
EP-14 | Callback Functions in JS ft. Event Listeners 🔥
1 parent 525f1a8 commit 6088804

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<h1 id="heading">Namaste JavaScript</h1>
10+
11+
<button id="clickMe">Click Me</button>
12+
13+
<script src="./Ep-14-Callback-Functions.js"></script>
14+
</body>
15+
</html>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// What is a callback Function in Javascript ?
2+
3+
// 🟨Functions are first class citizens in javascript
4+
// function pass into another function is called Callback Function.
5+
6+
setTimeout(() => {
7+
console.log("Timer⏰");
8+
},1000);
9+
function x (y) {
10+
console.log("x");
11+
y()
12+
}
13+
x(function z () {
14+
console.log("y");
15+
})
16+
17+
//-----------------------------------------------------------
18+
// Javascript is synchronous and single-threaded language
19+
20+
21+
// -----------------------------------------------------------
22+
// Blocking the main thread
23+
24+
25+
// -----------------------------------------------------------
26+
// Power of Callbacks ?
27+
28+
29+
// -----------------------------------------------------------
30+
// Deep about Event Listeners
31+
32+
33+
// -----------------------------------------------------------
34+
// Clossures demo with Event Listeners
35+
36+
37+
// -----------------------------------------------------------
38+
// Scope demo with Event Listeners
39+
40+
41+
// -----------------------------------------------------------
42+
// Garbage Collection & removeEventListeners
43+
44+
45+
46+
function attachEventListener () {
47+
let count = 0
48+
document.getElementById('clickMe').addEventListener("click",() => {
49+
console.log("Button Click",++count);
50+
});
51+
}
52+
attachEventListener()

0 commit comments

Comments
 (0)