“Функция JS Sleep” Ответ

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...
}
Energetic Eagle

JS Подождите

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

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

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!");
VahidTheGreat

JavaScript Sleep

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

Сон JS

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

Функция JS Sleep

/**
* Delay for a number of milliseconds
*/
function sleep(delay) {
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}
Roman Frolov

Ответы похожие на “Функция JS Sleep”

Вопросы похожие на “Функция JS Sleep”

Больше похожих ответов на “Функция JS Sleep” по JavaScript

Смотреть популярные ответы по языку

Смотреть другие языки программирования