Created
May 15, 2022 01:16
-
-
Save dement6d/934ee4a8c3baaf8978de1ec3933bcd67 to your computer and use it in GitHub Desktop.
Javascript sleep function with fancy output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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