469,266 Members | 2,061 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,266 developers. It's quick & easy.

How to make JavaScript wait for data?

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
Mar 19 '06 #1
8 37413


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Mar 19 '06 #2
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

Martin Honnen wrote:

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.

Mar 19 '06 #3
C.Joseph Drayton wrote:
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 ;)

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Mar 19 '06 #4
C.Joseph Drayton wrote:
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.p hp'. 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'); }

Mar 19 '06 #5
C.Joseph Drayton wrote:
Hi Marten,

When I use 'false' as the third parameter in makeRequest, I never a
response.
Please don't top post.

If you don't get a response, you are doing something wrong with your
request. Post some code.
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?

Martin told you - by using false as the third parameter to the open method.

--
Ian Collins.
Mar 19 '06 #6


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
}
--

Martin Honnen
http://JavaScript.FAQTs.com/
Mar 20 '06 #7
C.Joseph Drayton wrote:
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?


var theResponse = prompt('give me the data','')
Mar 20 '06 #8
Martin Honnen wrote:
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
Mar 22 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Federico Moschini [328594] | last post: by
5 posts views Thread by The Beast | last post: by
12 posts views Thread by m miller | last post: by
2 posts views Thread by Chris | last post: by
9 posts views Thread by paul | last post: by
1 post views Thread by monotreme | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.