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}
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}
9
10//using sleep()
11sleep(2000); //sleep for 2 seconds