-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEventLoopOutput.js
More file actions
43 lines (32 loc) · 855 Bytes
/
EventLoopOutput.js
File metadata and controls
43 lines (32 loc) · 855 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// The output of below code will be 0-8 but the terminal cursor does not get free.
// It means that that the output is displayed but the program kept running.
// It is because we have not cleared the interval.
let a = true;
let count = 0;
setTimeout(() => {
a = false;
}, 2000);
setInterval(() => {
if (a) {
console.log(count++);
}
}, 200);
// The output of the below code will infinite loop because the value a never becomes false.
// This happens due to Event loop.
let a = true;
let count = 0;
setTimeout(() => {
a = false;
}, 2000);
while (a) {
console.log(count++);
}
// The output of below code will be 0-8 and the cursor gets free meaning that,
// the program has fiished executing or running.
let count = 0;
let id = setInterval(() => {
console.log(count++);
}, 200);
setTimeout(() => {
clearInterval(id);
}, 2000);