The pause that doesn't

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arajunk@hiwaay.net

    #1

    The pause that doesn't

    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='javas cript'>
    document.write( 'here we go<br>');
    pausecomp(5000) ;
    document.write( 'now we are back');

    function pausecomp(Amoun t)
    {
    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>

  • Lee

    #2
    Re: The pause that doesn't

    arajunk@hiwaay. net said:[color=blue]
    >
    >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='javas cript'>
    >document.write ('here we go<br>');
    >pausecomp(5000 );
    >document.write ('now we are back');
    >
    >function pausecomp(Amoun t)
    >{
    >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>[/color]


    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()

    Comment

    • Fred Oz

      #3
      Re: The pause that doesn't

      arajunk@hiwaay. net wrote:[color=blue]
      > 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='javas cript'>
      > document.write( 'here we go<br>');
      > pausecomp(5000) ;
      > document.write( 'now we are back');
      >
      > function pausecomp(Amoun t)
      > {
      > 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>
      >[/color]

      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

      Comment

      • @(none)

        #4
        Re: The pause that doesn't

        Fred Oz wrote:[color=blue]
        > arajunk@hiwaay. net wrote:
        >[color=green]
        >> 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='javas cript'>
        >> document.write( 'here we go<br>');
        >> pausecomp(5000) ;
        >> document.write( 'now we are back');
        >>
        >> function pausecomp(Amoun t)
        >> {
        >> 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>
        >>[/color]
        >
        > 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.
        >[/color]
        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.

        Comment

        • Michael Winter

          #5
          Re: The pause that doesn't

          On Sun, 09 Jan 2005 13:30:17 +1100, @(none) <<""mas"@(none) ">> wrote:

          [snip]
          [color=blue]
          > Have a look at "http://www.synchro.net/docs/jsobjs.html" and search for
          > mswait.[/color]

          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.
          [color=blue]
          > I don't know that there is an inbuilt sleep or wait function in
          > javascript[/color]

          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.
          [color=blue]
          > the secret is to let the CPU do other things during the sleep period -
          > this may work.[/color]

          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.

          Comment

          • arajunk@hiwaay.net

            #6
            Re: The pause that doesn't

            >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?

            Comment

            • Lee

              #7
              Re: The pause that doesn't

              arajunk@hiwaay. net said:[color=blue]
              >[color=green]
              >>From what I read here I can't do what I want with JS. Using a form[/color]
              >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?
              >[/color]

              <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.getEle mentById("canva s").innerHTML=i nfo[info.pos];
              info.pos=(info. pos+1)%info.len gth;
              info.timer=setT imeout("showInf o()",2000);
              }

              </script>
              </head>
              <body onload="showInf o()">
              <div id="canvas" style="padding: 1em;background-color:yellow"></div>
              </body>
              </html>

              Comment

              • arajunk@hiwaay.net

                #8
                Re: The pause that doesn't

                Thanks Lee !! Nice job.

                Comment

                Working...