setInterval doesn't seem to like () in the function it invokes. Why?

本秂侑毒 提交于 2019-12-17 16:53:13

问题


When I execute the following, incidentController gets called after 10 seconds and continues to execute with no problems every 10 seconds:

// This works fine in nodejs v0.11.13
setInterval(incidentController, 10 * 1000);

function incidentController () {
 console.log ('executed'); 
}

However, this executes immediately and throws the following error on the second iteration:

//This doesn't. The parens which wrap (123) cause the error.

setInterval(incidentController(123), 10 * 1000);

function incidentController (someInt) {
 console.log ('executed: ', someInt); 
}

Error:

timers.js:287
    callback.apply(this, args);
            ^
TypeError: Cannot read property 'apply' of undefined
    at wrapper [as _onTimeout] (timers.js:287:13)
    at Timer.listOnTimeout (timers.js:133:15)

It seems like incidentController is/becomes undefined somehow. Can someone explain why this is expected behavior (I assume it is, anyway)?

I can work around this pretty easily, but I'm just curious why it behaves this way -- makes passing in parameter values a bit less convenient since I can't do it inside the setInterval statement itself.


回答1:


setInterval accepts a function object as the first parameter. But, when you do

setInterval(incidentController(123), 10 * 1000);

you are passing the result of invoking incidentController, which is undefined (In JavaScript, if a function doesn't return anything explicitly, then by default, undefined will be returned). That is why you are getting the error

Cannot read property 'apply' of undefined

It is trying to invoke the apply function on undefined.


makes passing in parameter values a bit less convenient since I can't do it inside the setInterval statement itself

No, sir. You can conveniently pass the parameter to the callback function in setInterval itself, like this

setInterval(incidentController, 10 * 1000, 123);

Now, when incidentController is invoked, after every 10 seconds, 123 will be passed as the first parameter to it.




回答2:


It is incorrect to use setInterval like

setInterval(incidentController(123), 10 * 1000);

because it expects a function as the first parameter(not an executed function with a result).

If you wish to pass a parameter to the call back, you should wrap the function call with an anonymous function like

setInterval(function(){incidentController(123)}, 10 * 1000);

See Pass parameters in setInterval function



来源:https://stackoverflow.com/questions/30419698/setinterval-doesnt-seem-to-like-in-the-function-it-invokes-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!