There's no need for the new
in this case.
setInterval()
is expecting a Function
, which the function
expression will already create.
setInterval(function(){console.log("hello")}, 1000);
// logs 'hello'
// logs 'hello'
// ...
The "hello"
is logged once with new
because it's invoking the Function
immediately as a constructor.
var o = new function () {
console.log("hello");
};
// logs "hello" during construction
console.log(typeof o); // "object"
And, the SyntaxError
is likely because setInterval()
, finding the argument isn't a Function
, is converting the instance Object
to a String
that it can eval
uate:
setInterval(new function () {
this.toString = function () {
return "console.log('foo');";
};
}, 1000);
// logs 'foo'
// logs 'foo'
// ...
Note: Since timers (setTimeout()
and setInterval()
) aren't actually standardized, the behavior in the previous snippet may not be consistent between engines.