By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,935 Members | 1,448 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,935 IT Pros & Developers. It's quick & easy.

is there a sleep functionality or similar solution in js

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
K Balusu wrote:
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
Jul 20 '05 #2

P: n/a
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
Jul 20 '05 #3

P: n/a
kb*****@yahoo.com (K Balusu) writes:
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 :)
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.