Skip to content

Commit c9169ba

Browse files
async ops and event loop
Signed-off-by: Arnav Gupta <[email protected]>
1 parent 98b886d commit c9169ba

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Lecture11/async-funcs/button.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
11+
<button id="btnClick">CLICK</button>
12+
<button id="btnWait">WAIT</button>
13+
<button id="btnCount">COUNT</button>
14+
<div id="result"></div>
15+
<div id="counter"></div>
16+
17+
<script>
18+
let result = document.getElementById('result')
19+
let counter = document.getElementById('counter')
20+
let btnClick = document.getElementById('btnClick')
21+
let btnWait = document.getElementById('btnWait')
22+
let btnCount = document.getElementById('btnCount')
23+
let c = 0
24+
25+
btnCount.onclick = function () {
26+
counter.innerText = ++c
27+
}
28+
29+
btnClick.onclick = function () {
30+
let start = new Date().getTime()
31+
setTimeout(() => {
32+
result.innerText = 'DONE!!!'
33+
console.log('Time taken = ', new Date().getTime() - start)
34+
}, 2000)
35+
}
36+
37+
btnWait.onclick = function () {
38+
let start = new Date().getTime();
39+
while (new Date().getTime() - 3000 < start) {
40+
/* do nothing */
41+
}
42+
}
43+
</script>
44+
45+
</body>
46+
</html>

Lecture11/async-funcs/callbacks.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
function waitASec(laterJob) {
3+
setTimeout(laterJob, 1000)
4+
}
5+
6+
console.log('started')
7+
8+
waitASec(function () {
9+
console.log('job done')
10+
})
11+
12+
console.log('ended')

0 commit comments

Comments
 (0)