Created
November 26, 2014 14:55
-
-
Save ondrek/ed4c60da3a2509a7effe to your computer and use it in GitHub Desktop.
my implementation of "the Sleep FN" in Javascript
This file contains hidden or 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
var sleep = function(howManyMs){ | |
howManyMs += +new Date(); // add timestamp | |
while (+new Date()<howManyMs){ ; } // just a NOOP | |
}; | |
// console: | |
// > console.log(+new Date(), "hello") | |
// > sleep(4000); | |
// > console.log(+new Date(), "world"); | |
// result: | |
// > hello 1417013576580 | |
// > world 1417013580580 <- exactly two seconds later |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice, why not use Date.now()?
Also, it's too bad that this will pin the CPU while sleeping.