“Tidur Javascript” 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 Javascript

function sleep(milliseconds) {
  const date = Date.now();
  let currentDate = null;
  do {
    currentDate = Date.now();
  } while (currentDate - date < milliseconds);
}

console.log("Hello");
sleep(2000);
console.log("World!");
VahidTheGreat

Tidur Javascript

//Javascipt is asyncronous so you can't pause/block code execution
//You can delay doing something with setTimeout
setTimeout(function(){
 	alert("Sup!"); 
}, 2000);//wait 2 seconds
Grepper

Tidur Javascript

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

async function delayedGreeting() {
  console.log("Hello");
  await sleep(2000);
  console.log("World!");
  await sleep(2000);
  console.log("Goodbye!");
}

delayedGreeting();
Distinct Dunlin

Tidur Javascript

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();
 Run code snippetHide results
Disgusted Dove

Tidur Javascript

console.log("Hello");
setTimeout(() => {  console.log("World!"); }, 5000);
Better Buffalo

Tidur Javascript

setTimeout(myFunction, 3000);
Funny Bunny

Jawaban yang mirip dengan “Tidur Javascript”

Pertanyaan yang mirip dengan “Tidur Javascript”

Lebih banyak jawaban terkait untuk “Tidur Javascript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya