2 ms·
It is not literally a sleep though, isn't setTimeout more like a creating a delayed event? (I am not a webdev)
by shultays 3y ago
It is not literally a sleep though, isn't setTimeout more like a creating a delayed event? (I am not a webdev)
- yencabulator 3y agoThat's Javascript for you. Don't want to block the one thread from doing other things in the meanwhile.
- Izkata 3y agoYou can't directly do a sleep in Javascript because it runs in the same thread as the UI - it would block the user from interacting with the page. This is effectively a sleep because after 5 seconds it's running the code in the passed-in function (not firing an event). The code in the function then resolves a promise, which runs other functions that can be specified later by what called the one using setTimeout.