You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Notes/17-setTimeout().md
+21-1Lines changed: 21 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,5 +34,25 @@ But JS waits for none. Goes to next line
34
34
35
35
- This raises a question. *Why not add more call stacks and make it multithreaded?*
36
36
- JS is a synch single threaded language. And thats its beauty. With just 1 thread it runs all pieces of code there. It becomes kind of an interpreter lang,
37
-
and runs code very fast inside browser (no need to wait for code to be compiled)
37
+
and runs code very fast inside browser (no need to wait for code to be compiled) (JIT - Just in time compilation). And there are still ways to do async operations as well.
38
38
39
+
### Now what if the timeout = 0sec
40
+
```
41
+
console.log("Start");
42
+
43
+
setTimeout(function cb() {
44
+
console.log("Callback");
45
+
}, 0);
46
+
47
+
console.log("End");
48
+
49
+
```
50
+
- Even though timer = 0s, the cb() has to go through the queue. Registers calback in webapi's env , moves to callback queue, and execute once callstack is empty
51
+
52
+
> Start
53
+
54
+
> End
55
+
56
+
> Callback
57
+
58
+
- This method of putting timer = 0, can be used to defer a less imp fun by a little so the more important fun (here printing "End") can take place
0 commit comments