Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between setInterval(func) and setInterval(function(){func()})

My ex boss had a weird bug where when he used setInterval with a long delay interval:

setInterval(func, 3000000 /*50 minutes*/);

Node.js crashed.

func can be even a simple function that simply console.log('something').

Someone suggested him to wrap an anonymous function around func, and it actually solved his issue.

As much as I know, it shouldn't make a difference and even considered to be a bad practice at least in browsers' javascript.

Is there a difference in Node.js between

  • setInterval(func, delay)
  • setInterval(function(){func()}, delay)

or is it a bug in Node.js?


UPDATE:

Bug report on GitHub

like image 724
gdoron is supporting Monica Avatar asked Jul 23 '15 19:07

gdoron is supporting Monica


1 Answers

To address your question directly: Yes, there is a difference. Not in the functionality of setInterval, but in the scope the function will have when it's run.

Scenario 1:

setInterval(function() {
  console.trace();
}, 3000000);

Every 50 minutes, a stack trace will be printed to console. In this case because the function console.trace has been called directly it will maintain the context of console.

Scenario 2:

setInterval(console.trace, 3000000);

This will throw an error because it will be invoked with the context of the scope that executes it, not console. To maintain context, you can pass a bound copy of the function:

setInterval(console.trace.bind(console), 3000000);

It doesn't seem the problem your boss had can be reproduced today. So, as others have suggested, it may be a problem with garbage collection. However, I would be more inclined to believe that the function your boss was calling depended on a specific context that was maintained via anonymous function but lost when passing an unbound function.

like image 84
rrowland Avatar answered Nov 07 '22 08:11

rrowland