What is the JavaScript version of sleep

0 votes

Is there a better way to engineer a sleep in JavaScript than the following pausecomp function ?

function pausecomp(millis) 
{ 
    var date = new Date(); 
    var curDate = null; 
    do { curDate = new Date(); } 
    while(curDate-date < millis); 
}



I want a real sleep in the middle of a function, and not a delay before a piece of code executes.

Feb 18, 2022 in Java by Rahul
• 9,680 points
417 views

1 answer to this question.

0 votes

JavaScript has evolved significantly since the time this query was raised. Here is the current best practice:-

function sleep(ms) { 
      return new Promise(resolve => setTimeout(resolve, ms)); 
}

Or as a one-liner:

await new Promise(r => setTimeout(r, 2000));

Or

const sleep = ms => new Promise(r => setTimeout(r, ms));

Use it as:

await sleep(<duration>);

Demo:

function sleep(ms) { 
        return new Promise(resolve => setTimeout(resolve, ms)); 
} 

async function demo() { 
            for (let i = 0; i < 5; i++) { 
              console.log(`Waiting ${i} seconds...`); 
                  await sleep(i * 1000); 
    } 
console.log('Done'); 
} 

demo();

 

Note that await can only be executed in functions prefixed with the async keyword, or at the top level of your script in an increasing number of environments. Additionally, wait only pauses the current async function. This means it does not block the execution of the rest of the script, which is what you want in the vast majority of the cases.

answered Feb 18, 2022 by Aditya
• 7,680 points

Related Questions In Java

0 votes
0 answers

What is the meaning of "$" sign in JavaScript

A dollar ($) symbol appears in the ...READ MORE

Nov 16, 2022 in Java by Nicholas
• 7,760 points
296 views
0 votes
0 answers

What is the use of the JavaScript 'bind' method?

What is the use of bind() in ...READ MORE

Nov 17, 2022 in Java by Nicholas
• 7,760 points
404 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
4,047 views
0 votes
2 answers

What is the use of @Override annotation in Java ? When do we use it ?

@Override annotation is used when we override ...READ MORE

answered Aug 14, 2019 in Java by Sirajul
• 59,230 points
3,402 views
0 votes
1 answer

Presenting docket dtates inside html page by javascript

Use the Docker Engine Api:Docker Engine API ...READ MORE

answered Jun 20, 2018 in Docker by DareDev
• 6,890 points
683 views
0 votes
1 answer

Migrating proxy npm repo in nexus 3

I don't think you can achieve this ...READ MORE

answered Jun 22, 2018 in DevOps Tools by DareDev
• 6,890 points
1,463 views
+1 vote
1 answer

What is the difference between JavaScript and Java

This quote rightly explains that 2 totally ...READ MORE

answered Jun 29, 2018 in Java by Daisy
• 8,140 points
730 views
0 votes
1 answer

Javascript require() function giving ReferenceError: require is not defined

RequireJS is a JavaScript file and module ...READ MORE

answered Feb 17, 2022 in Java by Aditya
• 7,680 points
3,805 views
0 votes
1 answer

How do I get the current date in JavaScript?

To ensure that you get the current ...READ MORE

answered Feb 18, 2022 in Java by Aditya
• 7,680 points
542 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP