1const sleep = (milliseconds) => {
2 return new Promise(resolve => setTimeout(resolve, milliseconds))
3}
4
5/*Use like so*/
6
7async function timeSensativeAction(){ //must be async func
8 //do something here
9 await sleep(5000) //wait 5 seconds
10 //continue on...
11}
1const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
2
3(async () => {
4 console.log('1');
5 await delay(1000);
6 console.log('2');
7})();
1// Sync version
2function sleep(sleepDuration){
3 var now = new Date().getTime();
4 while(new Date().getTime() < now + sleepDuration){ /* do nothing */ }
5}
1function sleep(milliseconds) {
2 var start = new Date().getTime();
3 for (var i = 0; i < 1e7; i++) {
4 if ((new Date().getTime() - start) > milliseconds){
5 break;
6 }
7 }
8}
9console.log('First');
10sleep(1000);
11console.log('Second');
1// sleep time expects milliseconds
2function sleep (time) {
3 return new Promise((resolve) => setTimeout(resolve, time));
4}
5
6// Usage!
7sleep(500).then(() => {
8 // Do something after the sleep!
9});
10