This is because whenever you pass a function inside setTimeout and call it, the passed function will be placed in the callBack based on the given delay in milliseconds. The functions within the callBack queue will be executed one after another in the order in which they were clicked. So, here, in your case, you block the flow of function code that is present inside the callBack queue by running the while . Therefore, the second test call takes 10 seconds to complete.
test(); //call 1 callBack queue ----> [function(){ while(){} }] test(); //call 2 callBack queue ----> [function(){ while(){} }, function(){ while(){} }]
Note: the callback queue will start when nothing has been executed in the call stack.
Itβs best to read this, Event Loop .
source share