Help with setTimeout...

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

    #1

    Help with setTimeout...

    I want to use a setTimeout to just loop for a number of seconds, then
    fall through to the next bit of code...can this be done by using some
    kind of null in the statement, or do I have to assign the code a name
    and call it from the setTimeout once it's done?

    Thanks so much!

  • MikeB

    #2
    Re: Help with setTimeout...


    "Mool" <ThePrisoner@Th eVillage.Net> wrote in message
    news:9bkve0d7d1 0ce332rmodtkbt4 ma2gmt2ur@4ax.c om...[color=blue]
    > I want to use a setTimeout to just loop for a number of seconds, then
    > fall through to the next bit of code...can this be done by using some
    > kind of null in the statement, or do I have to assign the code a name
    > and call it from the setTimeout once it's done?[/color]

    You could set a global variable to 'False' and have the function assigned to
    setTimeout change it to 'True', then in your loop check for the 'True' condition
    and break..
    [color=blue]
    > Thanks so much!
    >[/color]


    Comment

    • Lee

      #3
      Re: Help with setTimeout...

      Mool said:[color=blue]
      >
      >I want to use a setTimeout to just loop for a number of seconds, then
      >fall through to the next bit of code...can this be done by using some
      >kind of null in the statement, or do I have to assign the code a name
      >and call it from the setTimeout once it's done?[/color]

      That's not very clear, but it sounds as if you misunderstand
      what setTimeout() does. It doesn't loop. It doesn't cause
      execution to pause. It simply schedules a task to be performed
      after a delay. If you use it to schedule some kind of null,
      then that some kind of null will be scheduled to happen after a
      delay, but in the meantime, the next bit of code will already be
      running.

      In short, you do need to write the code as a function and schedule
      it using setTimeout().

      Comment

      • Dr John Stockton

        #4
        Re: Help with setTimeout...

        JRS: In article <9bkve0d7d10ce3 32rmodtkbt4ma2g mt2ur@4ax.com>, seen in
        news:comp.lang. javascript, Mool <ThePrisoner@Th eVillage.Net> posted at
        Sat, 10 Jul 2004 07:24:22 :[color=blue]
        >I want to use a setTimeout to just loop for a number of seconds, then
        >fall through to the next bit of code...can this be done by using some
        >kind of null in the statement, or do I have to assign the code a name
        >and call it from the setTimeout once it's done?[/color]

        You are asking for setTimeout to cause a delay in the current line of
        execution (you don't give a location, so I suppose we can rely on your
        words meaning what you intend). That was OK in single-process systems
        such as ran in plain DOS, but not appropriate in typical GUI
        environments.

        You should arrange for the initial line of execution to call
        setTimeout() and then to terminate naturally. The later code is
        provided as the first parameter of setTimeout(); AIUI it can be
        anything, but it should be a short string. In this case, a function
        call invoking the rest of the code is appropriate.

        Basically, a total redesign that changes very little.

        This is about the shortest possible example ( -88 in USA, -887 in EU ? )

        <input type=button onClick="setTim eout('alert(999 )', 1000)">

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        • Grant Wagner

          #5
          Re: Help with setTimeout...

          Lee wrote:
          [color=blue]
          > Mool said:[color=green]
          > >
          > >I want to use a setTimeout to just loop for a number of seconds, then
          > >fall through to the next bit of code...can this be done by using some
          > >kind of null in the statement, or do I have to assign the code a name
          > >and call it from the setTimeout once it's done?[/color]
          >
          > That's not very clear, but it sounds as if you misunderstand
          > what setTimeout() does. It doesn't loop. It doesn't cause
          > execution to pause. It simply schedules a task to be performed
          > after a delay. If you use it to schedule some kind of null,
          > then that some kind of null will be scheduled to happen after a
          > delay, but in the meantime, the next bit of code will already be
          > running.
          >
          > In short, you do need to write the code as a function and schedule
          > it using setTimeout().[/color]

          setTimeout() can be used as a hack to introduce a delay into Javascript
          code:

          function first() {
          // do stuff
          var t = setTimeout(seco nd, 3000);
          }
          function second() {
          // do more stuff
          var t = setTimeout(thir d, 5000);
          }
          function third() {
          // do yet more stuff
          }

          but anytime you are doing this, it is almost certainly the wrong way to
          handle whatever you are doing. Javascript is an event driven environment,
          waiting for a set period of time for something to happen (or not happen)
          is almost always the wrong way to solve whatever problem you think you're
          having.

          Provide more details of why you think you need to do this, I'm sure a
          solution can be provided that doesn't require your users to wait 3 or 5
          seconds for something to happen.

          --
          Grant Wagner <gwagner@agrico reunited.com>
          comp.lang.javas cript FAQ - http://jibbering.com/faq

          Comment

          Working...