“Tidur JS” Kode Jawaban

Tidur Javascript

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...
}
Energetic Eagle

Tidur JS

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

Tidur JS

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

Tidur JS

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

Tidur 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();
Long Lark

Tidur JS

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

// Use like so:

sleepFor(1000)
alert("hi")
Black Bird

Jawaban yang mirip dengan “Tidur JS”

Pertanyaan yang mirip dengan “Tidur JS”

Lebih banyak jawaban terkait untuk “Tidur JS” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya