setInterval function without arrow function

后端 未结 7 686
盖世英雄少女心
盖世英雄少女心 2021-01-02 07:20

I am learning about react components following the documentation https://facebook.github.io/react/docs/state-and-lifecycle.html

Why do we need to use arrow function

7条回答
  •  囚心锁ツ
    2021-01-02 08:12

    The first argument for setInterval is of type function. If you write this:

    this.timerID = setInterval(this.tick(), 1000);
    

    …then you don't pass a function, instead you execute the function this.tick immediately and then pass the value returned by that function call as an argument.

    You could write it like this:

    this.timerID = setInterval(this.tick, 1000);
    

    If you omit the parentheses, you pass a reference to your this.tick function, which is then executed by setInterval after 1000 milliseconds.

提交回复
热议问题