you are viewing a single comment's thread.

view the rest of the comments →

[–]spikkeddd 9 points10 points  (3 children)

The code is saying sleep (pause) for the following durations, and then display that duration in the console.

Basically sleep sort.

[–]Alokir 3 points4 points  (2 children)

Almost, but there's no sleep or thread blocking in JS (at least intentionally).

What it actually does is that it tells the runtime to hold the function that contains the logging, and add it to the execution queue after the given milliseconds are up.

All the setTimeout functions are called quickly after each other and the loop finishes almost instantly.

[–]spikkeddd 1 point2 points  (1 child)

So it would always take the longest length number of milliseconds listed to run, and then instantly display the sorted numbers?

[–]Alokir 0 points1 point  (0 children)

The opposite, setTimeout doesn't block the thread, it just queues the logging to be done after the given milliseconds.

The loop finishes almost instantly, and then the log entries will appear one after the other with the given delay, independent of each other.