Sleep

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xaradas

    Sleep

    How can I suspend the execution of a script for x seconds?

    Does exists something like sleep(x)?

    Thanks.

    ------------------------------------
    "Computer Science is no more about
    computers than astronomy is about
    telescopes."
    ::[E. W. Dijkstra]



    Posted from X-Privat Free NNTP server - www.x-privat.org
  • mark rees

    #2
    Re: Sleep

    You can use setTimeout, which calls a function after a set period of
    time. If it's possible to separate out the task you wish to delay into
    another function, this may be a solution.

    function delay(){
    alert('this happens now')
    setTimeout('aft erFiveSeconds() ',5000)
    }

    function afterFiveSecond s(){
    alert('this happens 5 seconds later')

    }

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!

    Comment

    • Brian

      #3
      Re: Sleep


      "Xaradas" <nospam@nospam. no> wrote in message news:3fcf05d8@x-privat.org...[color=blue]
      > How can I suspend the execution of a script for x seconds?
      >
      > Does exists something like sleep(x)?
      >
      > Thanks.
      >
      > ------------------------------------
      > "Computer Science is no more about
      > computers than astronomy is about
      > telescopes."
      > ::[E. W. Dijkstra]
      >
      >
      >
      > Posted from X-Privat Free NNTP server - www.x-privat.org[/color]

      No, Just like in most languages, a blocking sleep is system dependent, and
      not part of the language. The browser does not want you to do a blocking
      sleep, because it will block the script from completing, thus blocking the
      page from showing up. You can run setTimeout("fun ction()", msDelay), but
      that is a non-blocking delay... it will happen as an event.

      There may be a way to get around this.... though the only way I can think of
      off hand is a bit ugly, since it takes _ALL_ of the available processor,
      which is BAD... but here is the thought:

      var sleeping = true;
      setTimeout("sle eping = false", 1000); // 1 second
      while(sleeping) {}

      The best way to have a delay, is to redesign your system to not require a
      blocking delay, and use the non-blocking setTimeout.

      Brian



      Comment

      • Richard Cornford

        #4
        Re: Sleep

        "Brian" <BrianGenisio@n ospam.yahoo.com > wrote in message
        news:3fcf2b54$1 @10.10.0.241...
        <snip>[color=blue]
        >There may be a way to get around this.... though the only way I
        >can think of off hand is a bit ugly, since it takes _ALL_ of
        >the available processor, which is BAD... but here is the thought:
        >
        > var sleeping = true;
        > setTimeout("sle eping = false", 1000); // 1 second
        > while(sleeping) {}[/color]

        No, that is not going to work as most browsers will not interrupt
        running code in order to execute a setTimeout (indeed, I cannot think of
        any browsers that would). So the while loop will keep consuming 100% of
        processor power, and inhibiting most other activity in the browser,
        until the page is unloaded (Or the browser puts up a "a script on this
        page is causing the browser to run slowly ..." dialog.)
        [color=blue]
        >The best way to have a delay, is to redesign your system to not
        >require a blocking delay, and use the non-blocking setTimeout.[/color]

        Absolutely, or have the rest of the code triggered by some other event.

        Richard.


        Comment

        • Grant Wagner

          #5
          Re: Sleep

          Xaradas wrote:
          [color=blue]
          > How can I suspend the execution of a script for x seconds?
          >
          > Does exists something like sleep(x)?
          >
          > Thanks.[/color]

          In addition to the other responses, you can consider a design
          such as:

          function1() {
          // do stuff
          var t = setTimeout('fun ction2();', your_delay);
          }
          function2() {
          // do more stuff
          var t = setTimeout('fun ction3();', your_delay);
          }
          function function3() {
          // etc
          }

          However, there is hardly ever a need to do this. If the problem
          is that you are waiting for another frame or window to load,
          doing something like will be *extremely* unreliable, since if you
          set a long enough delay for the slowest of your client
          connections, you will inevitably create a huge delay for your
          fastest client connections.

          As Richard has pointed out, JavaScript is event-driven. Have the
          other window/frame send a signal to the opener or loader telling
          you it loaded successfully.

          If your problem is something else, perhaps expand on what problem
          you are trying to solve and we can provide a more appropriate
          solution.

          --
          | Grant Wagner <gwagner@agrico reunited.com>

          * Client-side Javascript and Netscape 4 DOM Reference available
          at:
          *


          * Internet Explorer DOM Reference available at:
          *
          Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


          * Netscape 6/7 DOM Reference available at:
          * http://www.mozilla.org/docs/dom/domref/
          * Tips for upgrading JavaScript for Netscape 7 / Mozilla
          * http://www.mozilla.org/docs/web-deve...upgrade_2.html


          Comment

          Working...