“Tidur di 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 di 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

versi 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 JS

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

fungsi tidur javascript

sleep = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds));

//        V  Must add this async keyword
sayHi = async () => {
  	// V Must include await
	await sleep(1000); // Sleep 1000 milliseconds (1 second)
  	console.log('Hi');
  	// Rest of code...
}
Pandata

Tidur JavaScript

console.log("Hello");
setTimeout(() => { console.log("World!"); }, 5000);
console.log("Goodbye!");
Hacked.fyi

Jawaban yang mirip dengan “Tidur di JavaScript”

Pertanyaan yang mirip dengan “Tidur di JavaScript”

Lebih banyak jawaban terkait untuk “Tidur di JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya