473,791 Members | 3,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 37735


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.getEle mentById('ifCom m').src='myPhpP age.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.mySubsequen tFunction()

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><ti tle>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 onreadystatecha nge 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.ope n('GET', 'whatever.php', false);
httpRequest.sen d(null); // this blocks as request is synchronous
if (httpRequest.st atus == 200) {
// use response here e.g. access
httpRequest.res ponseXML
}
--

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 onreadystatecha nge 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.ope n('GET', 'whatever.php', false);
httpRequest.sen d(null); // this blocks as request is synchronous
if (httpRequest.st atus == 200) {
// use response here e.g. access
httpRequest.res ponseXML
}


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
1821
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 - QtaGiaCons to obtain QtaInev (quantity that I have to ship because I have partially shipped) I wait for your suggestion. Thank you.
5
3898
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 it points to the CreateThread call. Here is my class, why is it doing this and how can I fix it? <TxdRxd.h>
4
4913
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 the report. Any suggestions? Thank you!
12
1508
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"; document.Form1.txtPartno.focus(); document.Form1.txtPartno.select(); }
2
1472
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 called so it looks like an image and not an animation. I have decided to put a wait function that will pause for 3 sec so that the animation will be loaded then the call the function for the long process. This is the <SCRIPT> section
9
1909
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 students at our school. However - we want to have multiple client PCs running a web interface that will allow them to type in their name / a message, and then this message appears on the "scroller" page, which will be on a projector.
1
3126
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 the javascript. onkeydown onkeypress or onkeyup will trigger it, but they are incorrect because they fire before the data gets in and the javascript thinks there's no data. I tried a setTimeout where it would wait 50 usec before it fired the...
5
4340
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 through a series of preset URL's in order to populate the caches of our website. so for ease lets say it has to surf through:
1
2026
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. So when I try to upload one photo, I get a lot of dummy files on my server and a lot of record in MySQL insted of one record. I guess the problem in JS: <SCRIPT LANGUAGE="JavaScript" TYPE="TEXT/JAVASCRIPT"> <!-- imageWait = new Image();...
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10201
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10147
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9987
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6770
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.