Skip to content

Instantly share code, notes, and snippets.

@ondrek
Created November 26, 2014 14:55
Show Gist options
  • Save ondrek/ed4c60da3a2509a7effe to your computer and use it in GitHub Desktop.
Save ondrek/ed4c60da3a2509a7effe to your computer and use it in GitHub Desktop.
my implementation of "the Sleep FN" in Javascript
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
@allain
Copy link

allain commented Nov 26, 2014

Nice, why not use Date.now()?

Also, it's too bad that this will pin the CPU while sleeping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment