Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Timing of function execution?

    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.
    hmm... ?

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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:
    Code:
    function a(){
        var
            x=b();
        return 'a:'+d(x,c());
    }
    
    function b(){
        return 'b';
    }
    
    function c(){
        return 'c:'+b();
    }
    
    function d(a,b){
        return 'd:'+a+','+b
    }
    
    a()
    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:
    JavaScript execution can in general always be simplified by a direct replacement of all expressions by their return value. Side effects are an exception.
    Last edited by liorean; 02-08-2004 at 10:24 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So, from that, I'll take it that they would both be executing at the same time.

    Thanks.
    hmm... ?

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    setTimeOut more or less start a 2nd execution thread that then await its set time before running the 1st function.

  • #5
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the illustration.

    Somehow, the whole 'return value' concept continues to evade my understanding -- but that's my fault.
    hmm... ?

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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:
    (((1+((2*3)/4))+5)-6)

    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.
    Last edited by liorean; 02-09-2004 at 12:30 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That explanation seems to have cleared it up for me.

    Great Job!
    hmm... ?

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Exception for this is setTimeout/setInterval
    Executing this code would display the "alert 2" first then "alert 1"
    Code:
    function display(){
      alert('alert 1');
    }
    setTimeout("display()",10);
    alert('alert 2');
    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.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's good to know; thanks.
    hmm... ?

  • #10
    wac
    wac is offline
    Regular Coder wac's Avatar
    Join Date
    Sep 2002
    Location
    Cary, North Carolina, USA
    Posts
    359
    Thanks
    2
    Thanked 0 Times in 0 Posts
    ...
    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.
    Wayne Christian


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •