0
0

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 3 years have passed since last update.

setTimeout(func(), delay) とsetTimeout(() => func(), delay) の違い

Posted at

つまづいたのでメモ

これの違い

setTimeout(func(), delay)
setTimeout(() => func(), delay)

説明

const log = () => console.log('aaa')
setTimeout(log(), 5000) // => 5sまたずに、 aaa 出力

ブラウザが左から解釈していくとします。
第一引数のlog()つまりconnsole.logを実行して、5秒後に tsでいう void、つまり何も返さずに実行されて終わります。

const log = () => console.log('aaa')
setTimeout(() => log(), 5000); // => 5s待って、 aaa 出力

ブラウザが左から解釈していくとします。
第一引数の() => log()つまりconnsole.logを実行することを定義された無名関数がセットされ、その関数が5秒後に実行されます。

参考

WindowOrWorkerGlobalScope.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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?