“Sleep JavaScript” Código de respuesta

Duerme de 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

Sleep 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

Sleep JavaScript

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

Sleep JavaScript

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

async function demo() {
    for (let i = 0; i < 5; i++) {
        console.log(`Waiting ${i} seconds...`);
        await sleep(i * 1000);
    }
    console.log('Done');
}

demo();
 Run code snippet
Famous Fowl

Sleep JavaScript

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

async function demo() {
    for (let i = 0; i < 5; i++) {
        console.log(`Waiting ${i} seconds...`);
        await sleep(i * 1000);
    }
    console.log('Done');
}

demo();
 Run code snippet
Famous Fowl

Sleep JavaScript

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

async function demo() {
    for (let i = 0; i < 5; i++) {
        console.log(`Waiting ${i} seconds...`);
        await sleep(i * 1000);
    }
    console.log('Done');
}

demo();
 Run code snippet
Famous Fowl

Respuestas similares a “Sleep JavaScript”

Preguntas similares a “Sleep JavaScript”

Más respuestas relacionadas con “Sleep JavaScript” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código