is there a sleep functionality or similar solution in js

K

K Balusu

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
 
E

Erwin Moller

K said:
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

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
 
C

Csaba2000

In addition, I recommend looking into window.setTimeout
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
 
L

Lasse Reichstein Nielsen

The problem we face now is that we don't have any sleep
functionality in javascript ( being event driven)

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(function(){
statement3;
statement4;},200);
}

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,callback) {
statement1;
statement2;
setTimeout(function(){
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,callback) {
statement1;
fetch('mydata.html',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.
and if we have a loop waiting for the reply, it will consume the cpu
cycles and the the frameB wont load.

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top