Skip to content

Instantly share code, notes, and snippets.

@dement6d
Created May 15, 2022 01:16
Show Gist options
  • Save dement6d/934ee4a8c3baaf8978de1ec3933bcd67 to your computer and use it in GitHub Desktop.
Save dement6d/934ee4a8c3baaf8978de1ec3933bcd67 to your computer and use it in GitHub Desktop.
Javascript sleep function with fancy output
function sleep(time) {
console.log(
'Sleeping for ' + (
time < 1000
? time + ((time == 1) ? "millisecond" : " milliseconds")
: time >= 60000
? (time >= 3600000)
? (time/3600000).toFixed(3) + (((time/3600000).toFixed(0) == 1) ? " hour" : " hours")
: ((time/1000)/60).toFixed(3) + (((time/1000)/60).toFixed(0) == 1 ? " minute" : " minutes")
: (time/1000).toFixed(3) + ((time/1000).toFixed(0) == 1 ? " second" : " seconds")
)
)
return new Promise(resolve => setTimeout(resolve, time));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment