Why doesn’t the following work in Firefox (v16)?
var t = setTimeout(foo(), 1000);
The error I get in the console is: “useless setTimeout call (missing quotes around argument?)”. Wrapping it in quotes doesn’t seem to do much, except making it render as a string (unsurprisingly).
It does however work fine when I wrap it in an anonymous function like so:
var t =
setTimeout(function(){
foo();
}, 1000);
But why is it necessary? Why doesn’t it explode in Webkit or Opera? Stroke of luck?
When you pass a method to
setTimeout(), it will be executed in global scope.thiswill point towindowat execution time. Read more HERE.If
foois not global, it won’t be found, ergoReferenceError.