Javascript sleep function

Unlike the setTimeOut (functionName, delay) method which places the functionName in a queue then execute it after delay miliseconds, The following function will hold up the web page for a time period.

function sleep(milliseconds) {
  const date =;
  let currentDate = null;
  do {
    currentDate =;
  } while (currentDate - date < milliseconds);

“wake up!!!” will be printed 3 seconds after “go to sleep”

console.log("go to sleep");
console.log("wake up!!!");