0
0

More than 3 years have passed since last update.

【JavaScript】setTimeoutを使って処理のスケジューリング

Posted at

JavaScriptで指定時間後に処理を実行するにはsetTimeoutを使用します。

■書き方

第二引数に与えられた実行タイミング(ミリ秒)で、第一引数に定義された処理内容を1度実行します

setTimeout(処理内容,実行タイミング(ミリ秒))

サンプルコード

以下は実行開始から5秒後にアラート表示される処理です。

example.js

var alertMessage = function(){
  alert("5秒経過しました!");
}
setTimeout(alertMessage, 5000);

関数に引数を渡す場合

example.js

function hello(name) {
  alert('Hello,' + name);
}

setTimeout(hello, 5000, 'Taro');

コールバック関数を使用する場合

example.js

function hello(name) {
  alert('Hello,' + name);
}

setTimeout(function() {
 hello('Taro');
}, 5000);

参照

window.setTimeout - Web API インターフェイス | MDN

0
0
0

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