Answers for "thread sleep javascript"

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
1

node wait 10 seconds

async function init() {
  console.log(1);
  await sleep(1000);
  console.log(2);
}

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

// one liner
let sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
Posted by: Guest on March-30-2020
1

javascript sleep settimeout

setTimeout(() => {  console.log("World!"); }, 2000);
Posted by: Guest on March-13-2020
1

sleeping in js

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

//using sleep()
sleep(2000); //sleep for 2 seconds
Posted by: Guest on June-22-2020
0

javascript sleep thread

var millisecondsToWait = 500;
setTimeout(function() {
    // Whatever you want to do after the wait
}, millisecondsToWait);
Posted by: Guest on September-08-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language