is there a sleep functionality or similar solution in js

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

    is there a sleep functionality or similar solution in js

    Hi all,
    We have to develop a small engine which the client uses. It supposed
    to work like this. Our engine resides in a frame (frameA) which will
    be loaded only once and it provides set of functions. The client can
    call these functions. We inturn should get the values from the server
    or set the values in the server and return with the values. The way we
    are planning to implement is that we will have have another frame
    (frameB hidden) and submit it whenever the client (frameClient) calls
    our function and wait till the page reloads and return with the value
    ( set in the reloaded page by the server). The problem we face now is
    that we don't have any sleep functionality in javascript ( being event
    driven) and if we have a loop waiting for the reply, it will consume
    the cpu cycles and the the frameB wont load.
    Is there a work around, or is there any other way to do this.
    TIA,
    Regards,
    Krishna Balusu
  • Erwin Moller

    #2
    Re: is there a sleep functionality or similar solution in js

    K Balusu wrote:
    [color=blue]
    > Hi all,
    > We have to develop a small engine which the client uses. It supposed
    > to work like this. Our engine resides in a frame (frameA) which will
    > be loaded only once and it provides set of functions. The client can
    > call these functions. We inturn should get the values from the server
    > or set the values in the server and return with the values. The way we
    > are planning to implement is that we will have have another frame
    > (frameB hidden) and submit it whenever the client (frameClient) calls
    > our function and wait till the page reloads and return with the value
    > ( set in the reloaded page by the server). The problem we face now is
    > that we don't have any sleep functionality in javascript ( being event
    > driven) and if we have a loop waiting for the reply, it will consume
    > the cpu cycles and the the frameB wont load.
    > Is there a work around, or is there any other way to do this.
    > TIA,
    > Regards,
    > Krishna Balusu[/color]

    Hi,

    Yes, the easiest way to implement this is use the code in the hidden frame
    call some function in your engine-frame, then do your stuff.
    You can use the onLoad-handler for this.
    So the trick is to let the serverresponse that fills the hidden frame also
    do the calling of a certain method.

    Should work pretty straightforward . If you need more help, just reply here
    and I will help you more.

    Good luck,
    Erwin Moller


    Comment

    • Csaba2000

      #3
      Re: is there a sleep functionality or similar solution in js

      In addition, I recommend looking into window.setTimeo ut
      for situations where the server doesn't return. Also, you
      may have race conditions if you allow multiple concurrent
      requests.

      Csaba Gabor from New York


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: is there a sleep functionality or similar solution in js

        kbalusu@yahoo.c om (K Balusu) writes:
        [color=blue]
        > The problem we face now is that we don't have any sleep
        > functionality in javascript ( being event driven)[/color]

        True. What you can do is to use setTimeout.
        Instead of
        function foo(x,y){
        statement1;
        statement2;
        sleep(200);
        statement3;
        statement4;
        }
        You write
        function foo(x,y){
        statement1;
        statement2;
        setTimeout(func tion(){
        statement3;
        statement4;},20 0);
        }

        The problem with this is that the original code returs, and if statement4
        is a return statement ("return result"), the value returned is lost.

        The typical solution to this is callbacks. Instead of expecting foo to
        return the result, you pass it a callback function that it calls when
        it is done [1]

        function foo(x,y,callbac k) {
        statement1;
        statement2;
        setTimeout(func tion(){
        statement3;
        callback(result );},200);
        }

        So why are we waiting? Probably because statement2 does an
        asynchroneous operation like fetching data over the network. Say,
        statement2 is calling the function "fetch('mydata. html')" which
        fetches the file in some way.

        We wait with setTimeout (and probably reschedule if the fetch isn't
        completed after 200 ms), because we don't know when the fetch is
        completed.

        What if we had given fetch a callback parameter too? Then we didn't
        have to wait actively, but could let fetch do the waiting:

        function foo(x,y,callbac k) {
        statement1;
        fetch('mydata.h tml',function() {
        statement3;
        callback(result );});
        }

        If all asynchroneous operations accepted a callback function, then you
        wouldn't need a sleep function (except for doing animation, and setInterval
        is really more suited for that).

        AFAIK, this is the approach the SVG DOM is taking.
        [color=blue]
        > and if we have a loop waiting for the reply, it will consume the cpu
        > cycles and the the frameB wont load.[/color]

        Busy waiting is never a good idea. :)

        I would still like to have sleep in the language, though.

        [1] This is really Continuation Passing Style programming :)
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        Working...