Web Analytics Made Easy -
StatCounter Timing of function execution? - CodingForum

Announcement

Collapse
No announcement yet.

Timing of function execution?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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; Feb 8, 2004, 05:24 PM.
    liorean <[[email protected]]>
    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

    Comment


    • #3
      So, from that, I'll take it that they would both be executing at the same time.

      Thanks.
      hmm... ?

      Comment


      • #4
        setTimeOut more or less start a 2nd execution thread that then await its set time before running the 1st function.

        Comment


        • #5
          Thank you for the illustration.

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

          Comment


          • #6
            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; Feb 8, 2004, 07:30 PM.
            liorean <[[email protected]]>
            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

            Comment


            • #7
              That explanation seems to have cleared it up for me.

              Great Job!
              hmm... ?

              Comment


              • #8
                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
                vBulletin Mods That Rock!

                Comment


                • #9
                  That's good to know; thanks.
                  hmm... ?

                  Comment


                  • #10
                    ...
                    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

                    Comment

                    Working...
                    X