“Schlaffunktion JS” Code-Antworten

JavaScript -Schlaf

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

JS wartet schlafen

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

console.log("Hello");
sleep(2000).then(() => { console.log("World!"); });
Crispyrice

Knoten warten 10 Sekunden

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));
just-saved-you-a-stackoverflow-visit

JavaScript -Schlaf

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

JavaScript -Schlaf

//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

Schlaffunktion JS

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

Ähnliche Antworten wie “Schlaffunktion JS”

Fragen ähnlich wie “Schlaffunktion JS”

Weitere verwandte Antworten zu “Schlaffunktion JS” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen