How to make JavaScript wait for data?

C

C.Joseph Drayton

Hi All,

I am calling a PHP script that does an operation then sends back a
result. I want JavaScript to wait until that result has been
recieved. I am using the following code. What can I do to stop it
from generating a 'Too much recursion' error?


function WaitForData()
{
thrspns = result.indexOf("au~~th");
if (result==null || result=="") { WaitForData(); }
}

Ciao . . . C.Joseph

That which a man buys too cheaply . . .
He esteems too lightly
 
M

Martin Honnen

C.Joseph Drayton wrote:

I am calling a PHP script that does an operation then sends back a
result. I want JavaScript to wait until that result has been
recieved. I am using the following code.
function WaitForData()
{
thrspns = result.indexOf("au~~th");
if (result==null || result=="") { WaitForData(); }

I don't see anything there that looks like "calling a PHP script".
If you use XMLHttpRequest to make your call to PHP then in theory you
can do that synchronously by using false as the third parameter to the
open method. But you need to be aware that blocking the script to wait
for a response might block the GUI thread of the window the script is
run in and that way in practice doing a synchronous request that waits
for data is not a good idea.
 
C

C.Joseph Drayton

Hi Marten,

When I use 'false' as the third parameter in makeRequest, I never a
response.

I understand what you say about making the GUI wait, but for my
purposes, it is imperative that no other processing occur until that
response is received and processed.

So is there a way to make JavaScript wait until that response is
received?

Thanks . . . C.Joseph

That which a man buys too cheaply . . .
He esteems too lightly
 
M

Matt Kruse

C.Joseph Drayton said:
I understand what you say about making the GUI wait, but for my
purposes, it is imperative that no other processing occur until that
response is received and processed.

This implies that you have not sufficiently designed your web app ;)
 
C

Csaba Gabor

C.Joseph Drayton said:
Hi Marten,

When I use 'false' as the third parameter in makeRequest, I never a
response.

I understand what you say about making the GUI wait, but for my
purposes, it is imperative that no other processing occur until that
response is received and processed.

So is there a way to make JavaScript wait until that response is
received?

I am doing this kind of thing frequently (getting info from the server
and waiting on the client till the info comes back), but I use a
completely different architecture, which is simple enough.

In the main document, put an <IFRAME src="" name=ifComm id=ifComm>.
Then, the script that requests the info should as its last line set the
src of the IFRAME to the appropriate thing (it is also possible to
POST, by specifying the target=ifComm), for example by
document.getElementById('ifComm').src='myPhpPage.php'. It is up to the
returned page to kick of the next step of the process by having an
onload script that does something like top.mySubsequentFunction()

Thus, waiting is accomplished not by looping (which may be blocking
other things from happening) until you see a certain value change, but
by code finishing until you have an event (the onload of the returned
document) kick off the next step in the process.

Csaba Gabor from Vienna

Example returned page:
<html><head><title>PHP server response</title></head>
<body onload=loaded()>
<script type='text/javascript'>
function loaded() { top.HoneyImBack(); }
</script>
Dummy text to see that server responded
</body></html>

where in the main page you might have
function HoneyImBack() { alert('waiting is over'); }
 
M

Martin Honnen

C.Joseph Drayton wrote:

When I use 'false' as the third parameter in makeRequest, I never a
response.

If you use false as the third parameter to the open call, then do not
set up an onreadystatechange event handler, rather put your code
processing the response after the send call as that call then blocks e.g.
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'whatever.php', false);
httpRequest.send(null); // this blocks as request is synchronous
if (httpRequest.status == 200) {
// use response here e.g. access
httpRequest.responseXML
}
 
C

C.Joseph Drayton

Martin said:
If you use false as the third parameter to the open call, then do not
set up an onreadystatechange event handler, rather put your code
processing the response after the send call as that call then blocks e.g.
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'whatever.php', false);
httpRequest.send(null); // this blocks as request is synchronous
if (httpRequest.status == 200) {
// use response here e.g. access
httpRequest.responseXML
}

Hi Martin,

That worked perfectly. Most of my pages call multiple makeRequest so
I created a function that is the global handler of all responses
from makeRequest and I am very happy with the result.

Thanks and have a great day.

Ciao . . . C.Joseph

That which a man buys too cheaply . . .
He esteems too lightly
 

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

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top