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}
1var delayInMilliseconds = 1000; //1 second
2
3setTimeout(function() {
4 //your code to be executed after 1 second
5}, delayInMilliseconds);
6
1function sleep(milliseconds) {
2 const date = Date.now();
3 let currentDate = null;
4 do {
5 currentDate = Date.now();
6 } while (currentDate - date < milliseconds);
7}
8
9console.log("Hello");
10sleep(2000);
11console.log("World!");
1function sleep(ms) {
2 return new Promise(resolve => setTimeout(resolve, ms));
3}
4
5console.log("Hello");
6sleep(2000).then(() => { console.log("World!"); });
7