setTimeout() ignored in loop

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

    setTimeout() ignored in loop

    I have a function called myFunction that can be called once from an
    onclick() or 100 times from a function called loopFunction which
    contains a for(var i=0;i<100;i++) loop.

    Within myFunction is a setTimeout() function like this:

    setTimeout(func tion(){shuffleD oor()},1500);

    Now, when I call myFunction from onclick() it waits 1.5 seconds and
    then calls the shuffleDoor function, just as I imagine it should.
    However when I call the function from loopFunction it is called 100
    times but there is no 1.5 second delay between calls.

    Why is this?

    Thanks in advance.
  • Joost Diepenmaat

    #2
    Re: setTimeout() ignored in loop

    Steve <stephen.joung@ googlemail.comw rites:
    I have a function called myFunction that can be called once from an
    onclick() or 100 times from a function called loopFunction which
    contains a for(var i=0;i<100;i++) loop.
    >
    Within myFunction is a setTimeout() function like this:
    >
    setTimeout(func tion(){shuffleD oor()},1500);
    >
    Now, when I call myFunction from onclick() it waits 1.5 seconds and
    then calls the shuffleDoor function, just as I imagine it should.
    However when I call the function from loopFunction it is called 100
    times but there is no 1.5 second delay between calls.
    >
    Why is this?
    Because you're doing it wrong. Your error is at line 15.

    Whenever you suspect a bug in a javascript/host implementation (or any
    other system that people have been using for years), it's your job to
    show that the bug is really there. Since you haven't done that, I'm
    going to assume the bug is really in your own code. Show the actual
    code, and we can probably find the actual problem.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

    Comment

    • Ugo

      #3
      Re: setTimeout() ignored in loop

      I have a function called myFunction that can be called once from an
      onclick() or 100 times from a function called loopFunction which
      contains a for(var i=0;i<100;i++) loop.
      >
      Within myFunction is a setTimeout() function like this:
      >
      setTimeout(func tion(){shuffleD oor()},1500);
      it's preferable:
      setTimeout( shuffleDoor, 1500 );
      Now, when I call myFunction from onclick() it waits 1.5 seconds and
      then calls the shuffleDoor function, just as I imagine it should.
      ok
      However when I call the function from loopFunction it is called 100
      times but there is no 1.5 second delay between calls.
      only the first one, I suppose
      Why is this?
      because the elapse time between single call of the loop is infinitesimal,
      setTimeout does not stall the script, the script continues immediately (not
      waiting for the timeout to expire); you schedule 100 contemporaneous
      actions.
      If you want execute the same function every 1,5 sec, the better way is to
      use setInterval

      Comment

      • Steve

        #4
        Re: setTimeout() ignored in loop

        On May 2, 9:29 pm, Ugo <priv...@nospam .itwrote:
        because the elapse time between single call of the loop is infinitesimal,
        setTimeout does not stall the script, the script continues immediately (not
        waiting for the timeout to expire); you schedule 100 contemporaneous
        actions.
        If you want execute the same function every 1,5 sec, the better way is to
        use setInterval
        Thank you Ugo for a very clear and useful answer.

        On May 2, 9:14 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
        Because you're doing it wrong. Your error is at line 15.
        >
        Whenever you suspect a bug in a javascript/host implementation (or any
        other system that people have been using for years), it's your job to
        show that the bug is really there. Since you haven't done that, I'm
        going to assume the bug is really in your own code. Show the actual
        code, and we can probably find the actual problem.
        This kind of smartass answer helps no-one. If you had bothered to read
        the question you would see that I was asking about the behavior of
        setTime() and not reporting a bug in javascript.

        If you don't have an helpful answer why bother answering?

        Comment

        • Joost Diepenmaat

          #5
          Re: setTimeout() ignored in loop

          Steve <stephen.joung@ googlemail.comw rites:
          On May 2, 9:14 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
          >
          >Because you're doing it wrong. Your error is at line 15.
          >>
          >Whenever you suspect a bug in a javascript/host implementation (or any
          >other system that people have been using for years), it's your job to
          >show that the bug is really there. Since you haven't done that, I'm
          >going to assume the bug is really in your own code. Show the actual
          >code, and we can probably find the actual problem.
          >
          This kind of smartass answer helps no-one. If you had bothered to read
          the question you would see that I was asking about the behavior of
          setTime() and not reporting a bug in javascript.
          I read the question. I just didn't notice that you actually did have a
          delay - since you didn't mention that - and so I was assuming that you
          had a real bug somewhere, instead of just the expected behaviour - as
          Ugo pointed out.
          If you don't have an helpful answer why bother answering?
          I did provide a helpful reply: without a good description of the
          problem and the relevant code, we're all just guessing.

          --
          Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: setTimeout() ignored in loop

            Ugo wrote:
            >setTimeout(fun ction(){shuffle Door()},1500);
            >
            it's preferable:
            setTimeout( shuffleDoor, 1500 );
            *Maybe*, and if, then only in this case. For example, you really do *not*
            want to write

            window.setTimeo ut(foo.bar, 1500);

            even if foo.bar does not have named arguments. shuffleDoor() could be the
            method of an object in the scope chain as well (you did not quote enough to
            assess that without further hassle).


            PointedEars
            --
            Use any version of Microsoft Frontpage to create your site.
            (This won't prevent people from viewing your source, but no one
            will want to steal it.)
            -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

            Comment

            • Gregor Kofler

              #7
              Re: setTimeout() ignored in loop

              Steve meinte:
              I have a function called myFunction that can be called once from an
              onclick() or 100 times from a function called loopFunction which
              contains a for(var i=0;i<100;i++) loop.
              >
              Within myFunction is a setTimeout() function like this:
              >
              setTimeout(func tion(){shuffleD oor()},1500);
              >
              Now, when I call myFunction from onclick() it waits 1.5 seconds and
              then calls the shuffleDoor function, just as I imagine it should.
              However when I call the function from loopFunction it is called 100
              times but there is no 1.5 second delay between calls.
              >
              Why is this?
              As Ugo has already explained. Maybe window.setInter val() with a branch
              for cancelling in the callback function is an option.

              Gregor


              --
              http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
              http://web.gregorkofler.com ::: meine JS-Spielwiese
              http://www.image2d.com ::: Bildagentur für den alpinen Raum

              Comment

              Working...