Skip to content

Commit a662ab1

Browse files
Update 17-setTimeout().md
1 parent 5a7ef11 commit a662ab1

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

Notes/17-setTimeout().md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,25 @@ But JS waits for none. Goes to next line
3434

3535
- This raises a question. *Why not add more call stacks and make it multithreaded?*
3636
- 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.
3838

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

Comments
 (0)