473,321 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

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
Jul 20 '05 #1
3 12183
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: ritterhaus | last post by:
Just a simple bit of code to toggle between two state at intervals... import time for i in range(4): print 'On' time.sleep(1) print 'Off' time.sleep(1) .... SHOULD toggle On and Off four...
21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
5
by: Erich Schreiber | last post by:
In the Python Library Reference the explanation of the time.sleep() function reads amongst others: > The actual suspension time may be less than that requested because > any caught signal will...
10
by: Alfonso Morra | last post by:
Hi, I need help witht he sleep function as follows. I need to be write som code to do the ff: 1. creates a new thread 2. (in the new thread), Sleep for x milliseconds 3. (in the new thread),...
0
by: Graeme M via DotNetMonster.com | last post by:
Hi, I'm using visual basic.net 2003 and in two separate projects now i have encountered a frustrating issue where all context menu functionality (copy, paste, delete etc) is disabled on a...
0
by: ste | last post by:
Please bear with this post.. its abit long winded.. but i thought it was important to describe what i am trying to achieve before i ask the questions (at the bottom) Thanks.....in advance ...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
7
by: multicherry | last post by:
Hi, Having searched for a way to fetch a window object by name, all I came across were answers along the line of... "All you have to do is say windowObj = window.open("blah", "name");" which...
21
by: JackCoke | last post by:
Hello. I am new at this so forgive me if there is a simple answer, although it is one I could not find. My VB 2003 program can scan a folder, determine the size of the file it finds, wait 30...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.