Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Really simple question!

  1. #1
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default Really simple question!

    Hi all,

    When one function follows another, like:

    someFunction();
    someOtherFunction();

    Does someOtherFunction() wait to execute until someFunction() is finished, or does someOtherFunction() run as soon as someFunction() starts running?

    Thanks!

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    I'm pretty sure that it goes through the first function, stays in there until it's executed all the code in there, and then moves on. It can't move ahead just randomly.

    Although the first function can call the second, so in that case it does jump, but it's still really in the first function.

    Please correct me if I'm wrong anyone.

    Hope that helps anyway,

    Jack.

  3. #3
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    For example:
    Code:
    var funca = function(){
      alert('Hello');
    };
    var funcb = function(){
      alert('Goodbye');
    };
    funca();
    funcb();
    With something like that, it would be called in order. The confirm, alert, and those functions are the kind I call 'delay' functions, they delay the whole page. The above code is just a longer way of saying:
    Code:
    alert('Hello');
    alert('Goodbye');
    Something like this wouldn't be a delay function:
    Code:
    var funca = function(){
      document.write('Hello');
    };
    var funcb = function(){
      document.write('Goodbye');
    };
    funca();
    funcb();
    I guess that kinda answered your question.. I don't have a complete answer though. =/
    Jeremy | jfein.net

  4. The Following User Says Thank You to Nile For This Useful Post:

    jlizarraga (01-20-2009)

  5. #4
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    Hmm... So what about this:

    Code:
    var holdup = true;
    var funca = function(){
        var timeout = setTimeout(function(){
            holdup = false;
        },1000);
        while(holdup){
            // Let's pretend it took some code in funca 1 second to process.
        }
        alert('Hello');
    };
    var funcb = function(){
        alert('Goodbye');
    };
    funca();
    funcb();
    In this case, which alert would you see first?

    edit: wait I'm retarded, I can test that myself. v_v

    edit2: lol, that froze my browser.
    Last edited by jlizarraga; 01-20-2009 at 02:35 AM. Reason: i'm retarded

  6. #5
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Code:
    while(true){
    
    }
    Will never stop... because true is always true. =/
    Jeremy | jfein.net

  7. #6
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    Even though there's the setTimeout that changes holdup to false after 1 second? The timer doesn't continue to count down during the while() stuff?

  8. #7
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    From looking here, I assume the while() function delays.
    Jeremy | jfein.net

  9. #8
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    Thanks for your info Nile.

  10. #9
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Glad to help.
    Jeremy | jfein.net

  11. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Javascript is single-threaded. That means that it's never really executing two things at the same time (which is why you don't have to worry about things like mutexes and semaphores in JS). When you setTimeout() a function, the runtime will wait until it isn't doing anything else before executing that function, even if that means that the function executes quite some time after it was scheduled to execute.

    All functions 'delay', as jlizarraga put it. The only difference between an alert() and a document.write() is that document.write() simply writes the data and then returns, while alert() waits to return until the presented box has been closed. This is standard flow of control in all imperative programming languages, not specific to Javascript.

    If you have some code:
    Code:
    01. function a() {
    02.   var i = 0;
    03. 
    04.   while (++i)
    05.     if (b(i))
    06.       return;
    07. }
    08. 
    09. function b(n) {
    10.   if (n > 2)
    11.     return true;
    12.   else
    13.     return false;
    14. }
    15. 
    16. function c() {
    17.   alert("Foo!");
    18.   alert("Bar!");
    19.   document.write("Baz!");
    20. }
    21. 
    22. setTimeout(c, 0);
    23. a();
    First, a pass is made over the script, at which point the top-level functions are parsed and pre-compiled. Then, the interpreter will start again at the top and scan down until something is executed, namely line 22. The interpreter will make a note to execute c when it is free, then continue down onto line 23. Line 23 tells it to jump to line 01, to begin execution of the function a(). It then executes line 02, defining i, and then onto the while statement. The value of ++i will always be true, so this is a prima facie infinite loop. However, on line 05, it calls b(1), jumping to 09 and proceeding to 10 and then on to 12 since the condition is false, skipping 11 entirely. At 13 the function is instructed to return false, and does so; control jumps back to a() at line 04, where we now know that the value of b(1) is false, so the condition is not met and we go back to line 04. ++i is still truthy (now having a value of 2), so we go through the whole thing again until we end up back here. This time, though, when we go through the loop with a value of 3, b(i) returns true (10 -> 11 -> 05) and thus we return. After this, the runtime has nothing else to do, so it executes c(), whose delay expired the moment we set it (since it is zero). Control jumps to 16 and then continues to 17 to alert("Foo!"), and remains in the alert() function until the user closes the alert, at which point it moves on to 18. The same thing happens there, and then it executes 19, where it jumps into document.write() and stays there until the provided text has been written to the page, before continuing on to return from the function (all functions have an implicit return; [which is equivalent to return undefined;] at the end).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  12. The Following User Says Thank You to Twey For This Useful Post:

    jlizarraga (01-20-2009)

Bookmarks

Posting Permissions

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