4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

コールバック関数作成

Posted at

##コールバック関数とは
処理が終わった後に実行される関数。
非同期処理の完了後実行したいとき等に使う。

javascript
function example() {
  console.log('callBack!!');
}

setTimeout(example, 5000); //5秒後に「callBack!!」

##自分でコールバック関数を作る

javascript
function example() {
  console.log('callBack!!');
}

function custom(_callback, _time) {
  setTimeout(_callback, _time);
}

custom(example, 5000); //5秒後に「callBack!!」

###コールバック関数に外部からの引数がある場合

javascript
function example(_msg) {
  console.log(_msg + ' callBack!!');
}

function custom(_callback, _msg, _time) {
  setTimeout(()=> {
    _callback(_msg);
  }, _time);
}

custom(example, 'argument!!', 5000); //5秒後に「argument!! callBack!!」
4
6
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
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?