The pause that doesn't

Discussion in 'Javascript' started by arajunk@hiwaay.net, Jan 8, 2005.

  1. Guest

    I was looking around for some code for a special purpose and found
    this. Seems it should print "here we go" , pause 5 seconds, print "now
    we are back". Instead, it pauses 5 seconds before printing both lines
    with no pause between. Can someone please explain why it behaves that
    way. I know in the case of "setTimeout" the only thing affected is the
    function, etc. appearing as "setTimeout" arguments. But, this is just
    straight code.

    <body>
    <script language='javascript'>
    document.write('here we go<br>');
    pausecomp(5000);
    document.write('now we are back');

    function pausecomp(Amount)
    {
    d = new Date() //today's date
    while (1<2)
    {
    mill=new Date() // Date Now
    diff = mill-d //difference in milliseconds
    if( diff > Amount ) {break;}
    }
    }
    </script>

    </body>
    , Jan 8, 2005
    #1
    1. Advertising

  2. Lee Guest

    said:
    >
    >I was looking around for some code for a special purpose and found
    >this. Seems it should print "here we go" , pause 5 seconds, print "now
    >we are back". Instead, it pauses 5 seconds before printing both lines
    >with no pause between. Can someone please explain why it behaves that
    >way. I know in the case of "setTimeout" the only thing affected is the
    >function, etc. appearing as "setTimeout" arguments. But, this is just
    >straight code.
    >
    ><body>
    ><script language='javascript'>
    >document.write('here we go<br>');
    >pausecomp(5000);
    >document.write('now we are back');
    >
    >function pausecomp(Amount)
    >{
    >d = new Date() //today's date
    >while (1<2)
    >{
    >mill=new Date() // Date Now
    >diff = mill-d //difference in milliseconds
    >if( diff > Amount ) {break;}
    >}
    >}
    ></script>
    >
    ></body>



    You can't expect the display to be updated by the time the document.write() call
    returns. That would cause trouble if you wanted to write part of a block in one
    call and the rest in a subsequent call, as is not uncommon.

    You can't be sure that the document will updated until you invoke
    document.close(), although some browsers will make assumptions about when you're
    done.

    And you certainly can't expect the browser to update the page when your machine
    is wasting 100% of its CPU capacity spinning its wheels waiting for 5 seconds to
    pass.

    If you want a delay, use setTimeout()
    Lee, Jan 8, 2005
    #2
    1. Advertising

  3. Fred Oz Guest

    wrote:
    > I was looking around for some code for a special purpose and found
    > this. Seems it should print "here we go" , pause 5 seconds, print "now
    > we are back". Instead, it pauses 5 seconds before printing both lines
    > with no pause between. Can someone please explain why it behaves that
    > way. I know in the case of "setTimeout" the only thing affected is the
    > function, etc. appearing as "setTimeout" arguments. But, this is just
    > straight code.
    >
    > <body>
    > <script language='javascript'>
    > document.write('here we go<br>');
    > pausecomp(5000);
    > document.write('now we are back');
    >
    > function pausecomp(Amount)
    > {
    > d = new Date() //today's date
    > while (1<2)
    > {
    > mill=new Date() // Date Now
    > diff = mill-d //difference in milliseconds
    > if( diff > Amount ) {break;}
    > }
    > }
    > </script>
    >
    > </body>
    >


    This is effectively a denial of service attack. It will hog the
    CPU by getting it to generate 2 or 3 dates per millisecond (or
    maybe 10 or 15 on faster machines than my slow old laptop)
    waiting for your script to timeout.

    I'm not sure that is what Javascript is for.

    --
    Fred
    Fred Oz, Jan 8, 2005
    #3
  4. @(none) Guest

    Fred Oz wrote:
    > wrote:
    >
    >> I was looking around for some code for a special purpose and found
    >> this. Seems it should print "here we go" , pause 5 seconds, print "now
    >> we are back". Instead, it pauses 5 seconds before printing both lines
    >> with no pause between. Can someone please explain why it behaves that
    >> way. I know in the case of "setTimeout" the only thing affected is the
    >> function, etc. appearing as "setTimeout" arguments. But, this is just
    >> straight code.
    >>
    >> <body>
    >> <script language='javascript'>
    >> document.write('here we go<br>');
    >> pausecomp(5000);
    >> document.write('now we are back');
    >>
    >> function pausecomp(Amount)
    >> {
    >> d = new Date() //today's date
    >> while (1<2)
    >> {
    >> mill=new Date() // Date Now
    >> diff = mill-d //difference in milliseconds
    >> if( diff > Amount ) {break;}
    >> }
    >> }
    >> </script>
    >>
    >> </body>
    >>

    >
    > This is effectively a denial of service attack. It will hog the
    > CPU by getting it to generate 2 or 3 dates per millisecond (or
    > maybe 10 or 15 on faster machines than my slow old laptop)
    > waiting for your script to timeout.
    >
    > I'm not sure that is what Javascript is for.
    >

    Have a look at "http://www.synchro.net/docs/jsobjs.html" and search for
    mswait.

    I don't know that there is an inbuilt sleep or wait function in
    javascript ( wait for flames !! ) - the secret is to let the CPU do
    other things during the sleep period - this may work.
    @(none), Jan 9, 2005
    #4
  5. On Sun, 09 Jan 2005 13:30:17 +1100, @(none) <<""mas"@(none)">> wrote:

    [snip]

    > Have a look at "http://www.synchro.net/docs/jsobjs.html" and search for
    > mswait.


    It's a fairly safe bet that the reference there is specifically for the
    Synchronet BBS system. There certainly isn't a mswait function in any
    browser object model that I'm aware of.

    > I don't know that there is an inbuilt sleep or wait function in
    > javascript


    No, there isn't. As both Lee and Fred said, a feature such as that would
    cause a browser to hang in most cases as script execution is synchronous.

    > the secret is to let the CPU do other things during the sleep period -
    > this may work.


    Precisely. Place the to-be-delayed code in a function and use setTimeout
    to call it after a certain period of time. However, this couldn't be done
    with document.write as the document stream would have closed resulting in
    replacement of, rather than addition to, the page.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
    Michael Winter, Jan 9, 2005
    #5
  6. Guest

    >From what I read here I can't do what I want with JS. Using a form
    textarea or the status line I can do it.... but, I don't care for
    either. I do have a java applet that will work. The application is
    very simple. Display a small bit of information followed by a pause
    and then display anothr bit of information, pause..... etc. Looping
    through an array . Anybody know how to do it?
    , Jan 11, 2005
    #6
  7. Lee Guest

    said:
    >
    >>From what I read here I can't do what I want with JS. Using a form

    >textarea or the status line I can do it.... but, I don't care for
    >either. I do have a java applet that will work. The application is
    >very simple. Display a small bit of information followed by a pause
    >and then display anothr bit of information, pause..... etc. Looping
    >through an array . Anybody know how to do it?
    >


    <html>
    <head>
    <title>demo</title>
    <script type="text/javascript">

    var info = [
    "From what I read here I can't do what I want with JS. Using a form",
    "textarea or the status line I can do it.... but, I don't care for",
    "either. I do have a java applet that will work. The application is",
    "very simple. Display a small bit of information followed by a pause",
    "and then display anothr bit of information, pause..... etc. Looping",
    "through an array . Anybody know how to do it?"
    ];

    function showInfo() {
    info.pos|=0;
    document.getElementById("canvas").innerHTML=info[info.pos];
    info.pos=(info.pos+1)%info.length;
    info.timer=setTimeout("showInfo()",2000);
    }

    </script>
    </head>
    <body onload="showInfo()">
    <div id="canvas" style="padding:1em;background-color:yellow"></div>
    </body>
    </html>
    Lee, Jan 11, 2005
    #7
  8. Guest

    Thanks Lee !! Nice job.
    , Jan 12, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Shohet
    Replies:
    10
    Views:
    1,885
    Kevin Spencer
    Jun 18, 2004
  2. GrantMagic

    pause a loop

    GrantMagic, Jul 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,966
  3. Leah

    Pause a web page?

    Leah, Mar 15, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    3,143
    Steve C. Orr [MVP, MCSD]
    Mar 15, 2006
  4. Rick
    Replies:
    4
    Views:
    41,530
    Roedy Green
    Oct 8, 2003
  5. E Tepp

    Pause Util Value is returned

    E Tepp, Dec 15, 2003, in forum: Java
    Replies:
    2
    Views:
    425
    Matthew Zimmer
    Dec 15, 2003
Loading...

Share This Page