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 in Java by Rahul
• 8,980 points
24 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 by Aditya
• 7,280 points

Related Questions In Java

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,120 points
2,297 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,170 points
1,562 views
0 votes
1 answer

What is the concept of Immutability for strings in Java ? Why are strings immutable ?

According to Effective Java, chapter 4, page 73, ...READ MORE

answered May 11, 2018 in Java by Rishabh
• 3,620 points
665 views
0 votes
2 answers

What is the use of final class in java?

In Java, items with the final modifier cannot be ...READ MORE

answered Oct 5, 2018 in Java by Daisy
• 8,120 points
452 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
196 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
764 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,120 points
230 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 in Java by Aditya
• 7,280 points
1,010 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 in Java by Aditya
• 7,280 points
20 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP