📜  javascript sleep - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:22.103000             🧑  作者: Mango

代码示例6
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function demo() {
  console.log('Taking a break...');
  await sleep(2000);
  console.log('Two seconds later, showing sleep in a loop...');

  // Sleep in loop
  for (let i = 0; i < 5; i++) {
    if (i === 3)
      await sleep(2000);
    console.log(i);
  }
}

demo();
 Run code snippetHide results