By oddities, I mean these two things:
- Functions in the first parameter need to be surrounded by
quotations, or else the delay is set to 0 (Everything is executed
instantly). - Everything after the timeout needs to have a delay after it as
well, or else it is executed prior to the timeout finishing.
If there’s a way around #2 – that would be awesome, but right now I’m just curious on this.
Short snippet to explain what I’m talking about:
for (var i=0; i<10; i++) setTimeout("addInput('.')",i*500);
setTimeout('addInput("</br>")',5100);
In the above, unless addInput(‘.’) is surrounded by quotations, the delay is ignored and the code is just executed; also unless I add a timeout to the second line, it will be executed before the first timeout is finished.
Not necessarily. In fact it is recommended to use the overload which takes directly a function pointer instead of a string to avoid the overhead of parsing it:
or its equivalent (warning: doesn’t work in IE):
The following setTimeout overload is the one that is most commonly used: