When you use 'function()' in the code sections above, do you mean that as a literal or as a stand in for the name of some function?There are two ways to create a function object: function declarations and function expressions. I explained the difference fairly recently on Usenet, so if I may, I'd prefer to just direct you to that post (http://www.google.co.uk/groups?hl=en&lr=&safe=off&frame=right&rnum=191&thl=665583981,665944740,665931301,665849489,665690272,665852990,665823390,0&seekm=39m3e8F61mervU1%40individual.net#link195). I also posted a few corrections and clarifications (http://www.google.co.uk/groups?hl=en&lr=&safe=off&frame=right&th=2049311b16479243&seekm=duqZd.4484%24QN1.3559%40text.news.blueyonder.co.uk#link1) which Google decided not to thread.
In what way would it be different, if in fact it would?Only the point at which the resulting function object would be available.
Especially, could the first method appear as part of another functionIt depends what you mean by "appear as part". You certainly could use an inner function to alter a local variable in an outer function:
/* This snippet is not executable as-is! */
function myFunction() {
var myBool = false;
function inner(){
if(condition) {myBool = true;}
}
while(!myBool) {
inner();
}
}However, your example is rather awkward because of how setTimeout works. In modern user agents, the function can take either a function reference, or a string. With the former, any function reference will do and it can take advantage of local variables in its scope chain:
function myFunction() {
var i = 0;
function inner() {
alert(i++);
}
setTimeout(inner, 2000);
}With the string version, the code is evaluated in global scope, so replacing the inner argument with the string, 'inner();', would cause an error because the inner function wouldn't exist. Unfortunately, some older user agents only accept this form, so it makes relying soley on the more flexible function version somewhat unwise. That said, there is a way to use both. An article (http://www.jibbering.com/faq/faq_notes/misc.html#mtSetTI) in the comp.lang.javascript FAQ notes explains this.
I've tried this with a do/while but the testPolling isn't generally a good idea for client-side scripts. Whilst a script is executing, a user agent will typically do nothing else making it appear that the UI is frozen. A better approach would be to use a callback that executes whatever you want once the image has finished loading. It could either be called from a load event listener on the image, or by periodically checking the complete property. However, neither approach is standardised - be aware of that. Particularly, before using the complete property, check that it's actually supported using a typeof operator test:
if(('boolean' == typeof img.complete) && !img.complete) {
/* complete property supported, but not set to true */
}Mike