Would a function called from within another complete itself before the caller function continued to its next line?
This doesn't seem to be true with timeouts, but does with loops (& its hard to tell, otherwise, because of the speed).
In general, though, I'd like to know if this would be the case.
02-08-2004, 10:55 PM
No. Function calls are expressions. They get called from a context, execute, and return a value, as well as the execution, to their context. Even setInterval and setTimeout do thus, but they have a side effect...
Something to illustrate:
Okay, how will this execute?
00. global: -> a
01. a: -> b
02. b: < 'b'
03. a: x <- 'b'
04. a: -> c
05. c: -> b
06. b: < 'b'
07. c: 'c:' + 'b'
08. c: < 'c:b'
09. a: -> d 'b' 'c:b'
10. d: 'd:'+'b' + ',' + 'c:b'
11. d: < 'd:b,c:b'
12. a: 'a:' + 'd:b,c:b'
13. a: < 'a:d:b,c:b'
14. global: 'a:d:b,c:b'
Or, to say it another way:
So, from that, I'll take it that they would both be executing at the same time.
02-08-2004, 11:45 PM
setTimeOut more or less start a 2nd execution thread that then await its set time before running the 1st function.
Thank you for the illustration.
Somehow, the whole 'return value' concept continues to evade my understanding -- but that's my fault. :D
02-09-2004, 01:19 AM
I think I can try to explain return values for you if you wish. Let me make an example using a bit of math:
Take the expression 1+2*3/4+5-6
Determine execution order from the operator precedence, and for the same precedence use left-to-right order:
Now, replace each paranthesis with the result of the operation taken within:
1. (((1+((2*3)/4))+5)-6) => (((1+(6/4))+5)-6)
2. (((1+(6/4))+5)-6) => (((1+1.5)+5)-6)
3. (((1+1.5)+5)-6) => ((2.5+5)-6)
4. ((2.5+5)-6) => (7.5-6)
5. (7.5-6) => 1.5
The same thing goes for all kinds of expressions, including function calls. The operator precedence and order is calculated, and each expression is replaced by it's result. That result is called the return value.
That explanation seems to have cleared it up for me.
Great Job! :thumbsup:
02-09-2004, 03:42 AM
Exception for this is setTimeout/setInterval
Executing this code would display the "alert 2" first then "alert 1"
setTimeout/setInterval do not halt the execution of any remaining scripts until the timeout has passed, it just schedules the expression or function for the specified time.
That's good to know; thanks.
setTimeOut more or less start a 2nd execution thread that then await its set time before running the 1st function
Does this mean that multiple setTimeOuts create multiple threads or are they all handled by a single thread which simply picks the next one to time out and runs it? I'm asking because we're using pushlets to implement 'unsolicited' updates from the server to the browser and we're starting to see performance issues with many events. I'm wondering if this is a threading issue or not.