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!
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!
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.
For example:
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:var funca = function(){ alert('Hello'); }; var funcb = function(){ alert('Goodbye'); }; funca(); funcb();
Something like this wouldn't be a delay function:Code:alert('Hello'); alert('Goodbye');
I guess that kinda answered your question.. I don't have a complete answer though. =/Code:var funca = function(){ document.write('Hello'); }; var funcb = function(){ document.write('Goodbye'); }; funca(); funcb();
Jeremy | jfein.net
jlizarraga (01-20-2009)
Hmm... So what about this:
In this case, which alert would you see first?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();
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
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?
Thanks for your info Nile.
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: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 implicitCode: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();return;[which is equivalent toreturn 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!
jlizarraga (01-20-2009)
Bookmarks