Answers for "sleep js"

29

javascript sleep

const sleep = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds))
}

/*Use like so*/

async function timeSensativeAction(){ //must be async func
  //do something here
  await sleep(5000) //wait 5 seconds
  //continue on...
}
Posted by: Guest on December-13-2019
13

sleep function javascript

How to get a JavaScript sleep function

First, you must sacrifice JS to the gods of asynchronous programming.
Then, go to grepper and ctrl c + v some code with no idea how to use it.
Give up.
Realise you didnt need it in the first place.
Die inside a little.
Realise you should have stayed with python.
Learn Java out of frustration.
Posted by: Guest on April-05-2021
1

sleep js

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

async function demo() {
  console.log('Taking a break...');
  await sleep(2000);
  console.log('Two seconds later, showing sleep in a loop...');

  // Sleep in loop
  for (let i = 0; i < 5; i++) {
    if (i === 3)
      await sleep(2000);
    console.log(i);
  }
}

demo();
Posted by: Guest on December-31-2020
2

sleep js

function sleepFor( sleepDuration ){
    var now = new Date().getTime();
    while(new Date().getTime() < now + sleepDuration){ /* do nothing */ } 
}

// Use like so:

sleepFor(1000)
alert("hi")
Posted by: Guest on April-11-2021
4

sleep js

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
Posted by: Guest on September-29-2020
1

sleep js

await new Promise(r => setTimeout(r, 2000));
Posted by: Guest on May-14-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language