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 6 of 6

Thread: setTimeout

  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts

    setTimeout

    In a javascript, assume there are three sequencial lines:

    var a = "1st line";

    setTimeout("2ndLine()", 1000);

    var b ="3rd line"

    ...


    function 2ndLine(){
    ...

    }

    Obviously, the order of the execution is:

    var a = "1st line";

    then, setTimeout("2ndLine()", 1000);

    Now once it hits this line, will it call the setTimeout function and wait for 1000ms, then execute the function 2ndLine(), then finally the var b ="3rd line"? OR

    it call the setTimeout function, put the 2ndLine() on hold (for 1000ms), but at the mean time, go ahead execute the 3rd line, i.e., var b ="3rd line" (so in this case, it is possible for the execution of 2ndLine() to happen after the 3rd line)?




    Thanks


    Scott
    Last edited by otnj2ee; 08-06-2008 at 12:41 AM.

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    It will put secondline(); on hold but continue to run the rest of your script, so yes if you code it like this secondline() will execute after the third line or however many other lines that it can process in that second
    Last edited by ninnypants; 08-06-2008 at 11:02 PM. Reason: errors

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    A simple test would tell you what happens

    Code:
    alert("1");
    window.setTimeout("alert('2')",1000);
    alert("3");
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    setTimeout() is not a break, nor a return. It is a simple bomb clock. While the chronometer ticks, life goes on and the rest of the code lines keep running till the Boom!...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Variable names cannot start with numbers.
    Trinithis

  • #6
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    @Trinithis you're completely right. My mistake


  •  

    Posting Permissions

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