Answers for "javascript sleep 1 second"

29

javascript sleep

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...
}
Posted by: Guest on December-13-2019
3

javascript wait 1 second

setTimeout(function(){ 
    console.log("Ready")
}, 1000);
Posted by: Guest on November-11-2020
6

javascript sleep

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!");
Posted by: Guest on December-09-2020
3

javascript sleep for 1 second

function delay(time) {
  return new Promise(resolve => setTimeout(resolve, time));
}

delay(1000).then(() => console.log('ran after 1 second1 passed'));
Posted by: Guest on October-25-2021
5

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
Posted by: Guest on July-23-2019
10

wait javascript

wait(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
 }
Posted by: Guest on June-19-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language