不带箭头功能的setInterval函数 [英] setInterval function without arrow function
问题描述
我正在根据文档 https://facebook了解关于React组件的信息.github.io/react/docs/state-and-lifecycle.html
为什么我们需要在这里使用箭头功能:
Why do we need to use arrow function here:
this.timerID = setInterval(() => this.tick(), 1000);
我为什么不能说(显然它不起作用)
Why can't I just say (obviously it doesn't work)
this.timerID = setInterval(this.tick(), 1000);
推荐答案
setInterval
的第一个参数的类型为function
.如果您这样写:
The first argument for setInterval
is of type function
. If you write this:
this.timerID = setInterval(this.tick(), 1000);
...然后,您不传递函数,而是立即执行函数this.tick
,然后将该函数调用返回的值作为参数传递.
…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.
您可以这样写:
this.timerID = setInterval(this.tick, 1000);
如果省略括号,则会传递对this.tick
函数的引用,该函数然后在1000毫秒后由setInterval
执行.
If you omit the parentheses, you pass a reference to your this.tick
function, which is then executed by setInterval
after 1000 milliseconds.
这篇关于不带箭头功能的setInterval函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!