Skip to content

Instantly share code, notes, and snippets.

@imshvc
Last active September 14, 2024 00:54
Show Gist options
  • Save imshvc/98511d798c001341716164419e7be7b8 to your computer and use it in GitHub Desktop.
Save imshvc/98511d798c001341716164419e7be7b8 to your computer and use it in GitHub Desktop.
JavaScript Sleep Function
/**
* Sleep for miliseconds
* @author Nurudin Imsirovic <[email protected]>
* @param {number} ms
* @returns {undefined}
*/
function sleep(ms) {
if (0 >= ms)
return
let start = Date.now()
do {} while (ms > (Date.now() - start))
}
// > console.log(Date.now())
// > sleep(3000)
// > console.log(Date.now())
//
// 1723950525789
// 1723950528789
// ^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment