I am using the following code to see if the JavaScript execution gets blocked while a synchronous function gets into a long loop.
function delayBySeconds(sec){
let start = now = Date.now();
while((now - start) < (sec * 1000)){
//console.log(now);
now = Date.now();
}
}
const seconds = 20;
delayBySeconds(seconds);
console.log(`${seconds} seconds passed`);
It prints the message immediately which shows the execution doesn't wait for the called function to complete its loop. How does that work? I mean if the delayBySeconds function works properly, how does the message get printed immediately? It's only experimental, of course, it doesn't make sense I would use it for some practical delay.