← Back to context

Comment by shultays

2 years ago

It is not literally a sleep though, isn't setTimeout more like a creating a delayed event? (I am not a webdev)

You 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.

That's Javascript for you. Don't want to block the one thread from doing other things in the meanwhile.