LoginSignup
1
3

More than 3 years have passed since last update.

【JavaScript】多分これが一番簡単なwait関数だと思います

Last updated at Posted at 2020-02-29

概要

タイトル通り、処理を待機する関数のコードを紹介します。ただし、関数内でしか使えません。

ソースコード

関数

async function wait(second) {
    return new Promise(resolve => setTimeout(resolve, 1000 * second));
}

使用例1

async function log() {
    console.log("3秒後にログを表示します。");
    await wait(3);
    console.log("3秒経過しました。");
}

log();

使用例2

(async () => {
    console.log("3秒後にログを表示します。");
    await wait(3);
    console.log("3秒経過しました。");
}
)()
1
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3