When using setTimeout
, you have to put the code you want to execute into a string:
setTimeout(\'alert(\"foobar!\");\', 1000);
If you don't need to call myGreatFunction
with any arguments, you should be able to pass setTimeout
a function reference:
setTimeout(myGreatFunction, 1000);
Also, you should always avoid passing setTimeout
code that it needs to evaluate (which is what happens when you wrap the code in quotes). Instead, wrap the code in an anonymous function:
setTimeout(function() {
// Code here...
}, 1000);
See the setTimeout page at the Mozilla Development Centre for more information.
Steve
Who said that it doesn't let you do it?
It does, the code -
setTimeout(function() { myFunction(); }, 1000);
is perfectly valid.