It doesn't, and in fact using it in quotes is not recommended as it uses eval()
.
The MDN defines window.setTimeout
as the following:
var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);
var timeoutID = window.setTimeout(code, delay);
where
timeoutID
is the numerical ID of the timeout, which can be used later with window.clearTimeout().
func
is the function you want to execute after delay milliseconds.
code
in the alternate syntax is a string of code you want to execute after delay milliseconds (using this syntax is not recommended for the same reasons as using eval()
)
Hence using functions
setTimeout(Tr, 5000)
and
setTimeout(function() { Tr(); });
would both work just as fine.
The latter with an anonymous function is used if you want to use parameters in your call - eg.:
var foo = 'bar';
setTimeout(function() { Tr('foo' + foo); }); // Will call Tr('foobar')
The reason why
setTimeout(Tr(), 5000)
does not work is because it first executes Tr()
and then calls setTimeout
with the result. If Tr()
returned the string 'foo'
, this call would call setTimeout('foo', 5000)
which makes no sense.