sleep 関数 - Javascript

  • 作成日:
  • 最終更新日:2025/06/25
">最終更新日:2025/05/25

sleep 関数

Javascript にはスリープ関数はありません。

const sleep = (time) => {
  return new Promise((r) => {
    setTimeout(r, time);
  })
}

(async () => {
  await sleep(5000);
  console.log("5秒経過");
  await sleep(3000);
  console.log("8秒経過");
})();