473,407 Members | 2,320 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,407 software developers and data experts.

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 37698


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Federico Moschini [328594] | last post by:
I have to make difference between2 data from a table in SQL. The page is made with Frontpage, and it extracts QtaGiaCons and Quantita from table "oclrighe". I have to make Quantita -...
5
by: The Beast | last post by:
I'm trying to create a thread to write to the serial port and I keep getting an odd error cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned long (__stdcall *)(void *)' and...
4
by: Aaron Fude | last post by:
Hi, I have report that takes about 5-10 seconds to generate depending on the load. I want to show the user a progress bar while it is being generated and when it's done, forward the browser to...
12
by: m miller | last post by:
I have the following javascript in my html: <script language="javascript"> function setfocus() { if (document.body.style.cursor="default") { document.body.style.cursor="wait";...
2
by: Chris | last post by:
Hi, I have a Javascript function that loads a page with a progress bar for long process. The progress bar is a gif animation and for some reason it the animation is stuck when the function is...
9
by: paul | last post by:
Hi All, We have a small dilemma. We have the following page: http://giggsey.com/m00Cow.php (don't ask about the content) that we want to turn into an interactive application for some new intake...
1
by: monotreme | last post by:
I have a form one input. If you set it up with onchange="somejavascript...." and you type into the input field it does the javascript. If you scan a barcode into the input box it doesn't trigger...
5
by: Seer | last post by:
Hi. This is driving me crazy so I hope someone will help! I am using chickenfoot and javascript to try and achieve something that should be really simple. Basically a script that navigates...
1
by: Al7 | last post by:
Hello, I have wrote code that uploads files from user's hard disks to the server and everything works fine in Firefox and Safari, but in Internet Explorer it uploads dummy files and cycles that....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.