javascript wait

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stephen.durkin@gmail.com

    javascript wait

    I've gathered that javascript doesn't have a handy wait() function, so
    I made the following function makeDelay() to make use of PHP's sleep(),
    which then does the waiting, and responds with "done"...my problem is
    that the javascript doesn't wait for makeDelay(secon ds) to finish...?

    makeDelay(5);
    iDontWaitForNob ody();

    function makeDelay(secon ds) {

    url="make_delay .php?delay=" + seconds;
    http.open("GET" ,url,true);

    loadStatus=docu ment.getElement ById("loadStatu s");

    http.onreadysta techange=functi on() {
    if (http.readyStat e==1) {
    loadStatus.src= "balls/load1.gif";
    }
    if (http.readyStat e==2) {
    loadStatus.src= "balls/load2.gif";
    }
    if (http.readyStat e==3) {
    loadStatus.src= "balls/load3.gif";
    }
    if (http.readyStat e==4) {
    if(http.respons eText == "done")
    loadStatus.src= "balls/load0.gif";
    }
    }
    http.send(null) ;
    }

  • Rik

    #2
    Re: javascript wait

    stephen.durkin@ gmail.com wrote:
    I've gathered that javascript doesn't have a handy wait() function, so
    I made the following function makeDelay() to make use of PHP's
    sleep(), which then does the waiting, and responds with "done"...my
    problem is that the javascript doesn't wait for makeDelay(secon ds) to
    finish...?
    Don't do that.
    Use setTimeout();

    In this case:
    setTimeout("iDo ntWaitForNobody ()",5000);
    --
    Rik Wasmus


    Comment

    • Peter Michaux

      #3
      Re: javascript wait


      stephen.durkin@ gmail.com wrote:
      I've gathered that javascript doesn't have a handy wait() function,
      I think you mightu be looking for JavaScript's setTimeout()

      Peter

      Comment

      • Peter Michaux

        #4
        Re: javascript wait


        Rik wrote:
        stephen.durkin@ gmail.com wrote:
        I've gathered that javascript doesn't have a handy wait() function, so
        I made the following function makeDelay() to make use of PHP's
        sleep(), which then does the waiting, and responds with "done"...my
        problem is that the javascript doesn't wait for makeDelay(secon ds) to
        finish...?
        >
        Don't do that.
        Use setTimeout();
        >
        In this case:
        setTimeout("iDo ntWaitForNobody ()",5000);
        or even just

        setTimeout(iDon tWaitForNobody, 5000);

        Comment

        • Randy Webb

          #5
          Re: javascript wait

          Peter Michaux said the following on 10/24/2006 11:03 PM:
          Rik wrote:
          >stephen.durkin@ gmail.com wrote:
          >>I've gathered that javascript doesn't have a handy wait() function, so
          >>I made the following function makeDelay() to make use of PHP's
          >>sleep(), which then does the waiting, and responds with "done"...my
          >>problem is that the javascript doesn't wait for makeDelay(secon ds) to
          >>finish...?
          >Don't do that.
          >Use setTimeout();
          >>
          >In this case:
          >setTimeout("iD ontWaitForNobod y()",5000);
          >
          or even just
          >
          setTimeout(iDon tWaitForNobody, 5000);
          >
          Neither of which does what the OP wanted - duplicating PHP's wait()
          function which pauses execution of *all* code until the time is up.

          --
          Randy
          Chance Favors The Prepared Mind
          comp.lang.javas cript FAQ - http://jibbering.com/faq
          Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

          Comment

          • Rik

            #6
            Re: javascript wait

            Peter Michaux wrote:
            Rik wrote:
            >stephen.durkin@ gmail.com wrote:
            >>I've gathered that javascript doesn't have a handy wait() function,
            >>so I made the following function makeDelay() to make use of PHP's
            >>sleep(), which then does the waiting, and responds with "done"...my
            >>problem is that the javascript doesn't wait for makeDelay(secon ds)
            >>to finish...?
            >>
            >Don't do that.
            >Use setTimeout();
            >>
            >In this case:
            >setTimeout("iD ontWaitForNobod y()",5000);
            >
            or even just
            >
            setTimeout(iDon tWaitForNobody, 5000);
            True,

            I usually follow this procedure though:
            If it calls a function directly, quote it.
            If it calls a function which name is in a variable, don't quote it
            offcourse.

            That way it's easier to backtrace everything if there's an error.

            Wether this is good practise or not is something I don't know, I usually
            don't do that much javascript :)

            --
            Rik Wasmus


            Comment

            • Rik

              #7
              Re: javascript wait

              Randy Webb wrote:
              Peter Michaux said the following on 10/24/2006 11:03 PM:
              >Rik wrote:
              >>stephen.durkin@ gmail.com wrote:
              >>>I've gathered that javascript doesn't have a handy wait()
              >>>function, so I made the following function makeDelay() to make use
              >>>of PHP's sleep(), which then does the waiting, and responds with
              >>>"done"...m y problem is that the javascript doesn't wait for
              >>>makeDelay(se conds) to finish...?
              >>Don't do that.
              >>Use setTimeout();
              >>>
              >>In this case:
              >>setTimeout("i DontWaitForNobo dy()",5000);
              >>
              >or even just
              >>
              >setTimeout(iDo ntWaitForNobody , 5000);
              >>
              >
              Neither of which does what the OP wanted - duplicating PHP's wait()
              function which pauses execution of *all* code until the time is up.
              Hmmmz, you've got a point.
              If it's a simple halt in a function, you could call a second function that
              takes over like this. If one wants to stop all code, that would require all
              events fired be focusing/moving/etc. will have to stop.

              I really don't know enough js to even start to consider how that second
              option could be implemented...
              --
              Rik Wasmus


              Comment

              • Peter Michaux

                #8
                Re: javascript wait

                Randy Webb wrote:
                Peter Michaux said the following on 10/24/2006 11:03 PM:
                Rik wrote:
                stephen.durkin@ gmail.com wrote:
                >I've gathered that javascript doesn't have a handy wait() function, so
                >I made the following function makeDelay() to make use of PHP's
                >sleep(), which then does the waiting, and responds with "done"...my
                >problem is that the javascript doesn't wait for makeDelay(secon ds) to
                >finish...?
                Don't do that.
                Use setTimeout();
                >
                In this case:
                setTimeout("iDo ntWaitForNobody ()",5000);
                or even just

                setTimeout(iDon tWaitForNobody, 5000);
                >
                Neither of which does what the OP wanted - duplicating PHP's wait()
                function which pauses execution of *all* code until the time is up.
                True but since the OP didn't state the goal it could be that setTimeout
                does exactly what he wants in this case. But maybe not.

                Using the Internet to create the wait seems like a bad way to go since
                the timing wouldn't be controllable.

                I bet there is a setTimeout solution if the OP knows about it and
                thinks about the problem with setTimeout in mind.

                Peter

                Comment

                • Randy Webb

                  #9
                  Re: javascript wait

                  Peter Michaux said the following on 10/24/2006 11:41 PM:
                  Randy Webb wrote:
                  >Peter Michaux said the following on 10/24/2006 11:03 PM:
                  >>Rik wrote:
                  >>>stephen.durkin@ gmail.com wrote:
                  >>>>I've gathered that javascript doesn't have a handy wait() function, so
                  >>>>I made the following function makeDelay() to make use of PHP's
                  >>>>sleep(), which then does the waiting, and responds with "done"...my
                  >>>>problem is that the javascript doesn't wait for makeDelay(secon ds) to
                  >>>>finish... ?
                  >>>Don't do that.
                  >>>Use setTimeout();
                  >>>>
                  >>>In this case:
                  >>>setTimeout(" iDontWaitForNob ody()",5000);
                  >>or even just
                  >>>
                  >>setTimeout(iD ontWaitForNobod y, 5000);
                  >>>
                  >Neither of which does what the OP wanted - duplicating PHP's wait()
                  >function which pauses execution of *all* code until the time is up.
                  >
                  True but since the OP didn't state the goal it could be that setTimeout
                  does exactly what he wants in this case. But maybe not.
                  >
                  Using the Internet to create the wait seems like a bad way to go since
                  the timing wouldn't be controllable.
                  It's a bad idea, period, because a true wait() locks up the browser. The
                  time is very controllable though. And, that is very easy to test.

                  --
                  Randy
                  Chance Favors The Prepared Mind
                  comp.lang.javas cript FAQ - http://jibbering.com/faq
                  Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

                  Comment

                  • Randy Webb

                    #10
                    Re: javascript wait

                    Rik said the following on 10/24/2006 11:36 PM:
                    Randy Webb wrote:
                    >Peter Michaux said the following on 10/24/2006 11:03 PM:
                    >>Rik wrote:
                    >>>stephen.durkin@ gmail.com wrote:
                    >>>>I've gathered that javascript doesn't have a handy wait()
                    >>>>function, so I made the following function makeDelay() to make use
                    >>>>of PHP's sleep(), which then does the waiting, and responds with
                    >>>>"done"... my problem is that the javascript doesn't wait for
                    >>>>makeDelay(s econds) to finish...?
                    >>>Don't do that.
                    >>>Use setTimeout();
                    >>>>
                    >>>In this case:
                    >>>setTimeout(" iDontWaitForNob ody()",5000);
                    >>or even just
                    >>>
                    >>setTimeout(iD ontWaitForNobod y, 5000);
                    >>>
                    >Neither of which does what the OP wanted - duplicating PHP's wait()
                    >function which pauses execution of *all* code until the time is up.
                    >
                    Hmmmz, you've got a point.
                    If it's a simple halt in a function, you could call a second function that
                    takes over like this. If one wants to stop all code, that would require all
                    events fired be focusing/moving/etc. will have to stop.
                    Not really. If you had a true wait(), then all execution *will* stop
                    because that is what wait() does. After the sleep is finished, its a rat
                    race as to what events will still fire and in what order, but they
                    should fire in the order they were initiated.

                    --
                    Randy
                    Chance Favors The Prepared Mind
                    comp.lang.javas cript FAQ - http://jibbering.com/faq
                    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

                    Comment

                    • Laurent Bugnion

                      #11
                      Re: javascript wait

                      Hi,

                      Rik wrote:
                      Peter Michaux wrote:
                      >or even just
                      >>
                      >setTimeout(iDo ntWaitForNobody , 5000);
                      >
                      True,
                      >
                      I usually follow this procedure though:
                      If it calls a function directly, quote it.
                      If it calls a function which name is in a variable, don't quote it
                      offcourse.
                      That's not what setTimeout(iDon tWaitForNobody, 5000) does. It gives to
                      the setTimeout function a reference to the iDontWaitForNob ody function
                      (which is an object, because in JavaScript, functions are objects). If
                      you're familiar with C#, think of it as a delegate (though it's actually
                      much more direct, and also much older than delegates).

                      For parameter-less methods, this way is faster, since the statement
                      doesn't need to be parsed and evaluated.

                      For methods with parameters, it doesn't work, but then you can use closure.

                      HTH,
                      Laurent
                      --
                      Laurent Bugnion, GalaSoft
                      Software engineering: http://www.galasoft-LB.ch
                      PhotoAlbum: http://www.galasoft-LB.ch/pictures
                      Support children in Calcutta: http://www.calcutta-espoir.ch

                      Comment

                      • Laurent Bugnion

                        #12
                        Re: javascript wait

                        Hi,

                        stephen.durkin@ gmail.com wrote:
                        I've gathered that javascript doesn't have a handy wait() function, so
                        I made the following function makeDelay() to make use of PHP's sleep(),
                        which then does the waiting, and responds with "done"...my problem is
                        that the javascript doesn't wait for makeDelay(secon ds) to finish...?
                        >
                        makeDelay(5);
                        iDontWaitForNob ody();
                        >
                        function makeDelay(secon ds) {
                        >
                        url="make_delay .php?delay=" + seconds;
                        http.open("GET" ,url,true);
                        >
                        loadStatus=docu ment.getElement ById("loadStatu s");
                        >
                        http.onreadysta techange=functi on() {
                        if (http.readyStat e==1) {
                        loadStatus.src= "balls/load1.gif";
                        }
                        if (http.readyStat e==2) {
                        loadStatus.src= "balls/load2.gif";
                        }
                        if (http.readyStat e==3) {
                        loadStatus.src= "balls/load3.gif";
                        }
                        if (http.readyStat e==4) {
                        if(http.respons eText == "done")
                        loadStatus.src= "balls/load0.gif";
                        }
                        }
                        http.send(null) ;
                        }
                        The reason why it doesn't work is that you call the web service method
                        in an asynchronous way. XmlHttpRequest also supports synchronous calls,
                        but I really, really recommend against it in most cases. JavaScript is
                        not multi-threaded, so if you let one thread sleeps, everything sleeps.

                        Use timers instead, like others recommended.

                        HTH,
                        Laurent
                        --
                        Laurent Bugnion, GalaSoft
                        Software engineering: http://www.galasoft-LB.ch
                        PhotoAlbum: http://www.galasoft-LB.ch/pictures
                        Support children in Calcutta: http://www.calcutta-espoir.ch

                        Comment

                        • Rik

                          #13
                          Re: javascript wait

                          Laurent Bugnion wrote:
                          (which is an object, because in JavaScript, functions are objects).
                          Check, you're right.
                          --
                          Rik Wasmus


                          Comment

                          • Peter Tran

                            #14
                            Re: javascript wait



                            If you really want to do it the way you are doing it, just change the
                            following line:

                            From:
                            http.open("GET" ,url,true);

                            To:
                            http.open("GET" ,url,false);

                            This will make the call syncrhonous...

                            *** Sent via Developersdex http://www.developersdex.com ***

                            Comment

                            • Laurent Bugnion [MVP]

                              #15
                              Re: javascript wait

                              Hi,

                              Peter Tran wrote:
                              >
                              If you really want to do it the way you are doing it, just change the
                              following line:
                              >
                              From:
                              http.open("GET" ,url,true);
                              >
                              To:
                              http.open("GET" ,url,false);
                              >
                              This will make the call syncrhonous...
                              You should quote what you reply to.

                              Also, I know how to make a synchronous call. You're replying to the
                              wrong person. That all makes your post quite difficult to understand.

                              HTH,
                              Laurent
                              --
                              Laurent Bugnion [MVP ASP.NET]
                              Software engineering, Blog: http://www.galasoft-LB.ch
                              PhotoAlbum: http://www.galasoft-LB.ch/pictures
                              Support children in Calcutta: http://www.calcutta-espoir.ch

                              Comment

                              Working...