473,396 Members | 1,816 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,396 software developers and data experts.

AJAX + delay

I came across a problem and googling for an answer didn't help.

What I want to do is run an AJAX script that sets a hidden variable on
the form. I call the AJAX script from a javascript function. After the
call, I check the value of that hidden variable and proceed according to
whether it is zero or one.

The problem is that the AJAX call does not complete before the test.
So, I put in a delay after the call to the AJAX function. I even tried
5000 msec. Meanwhile, I looked at the value of the hidden variable
using Firebug. What happens is that the variable does not change until
the delay is completed and the value that the function obtains is the
old value.

It seems as if the AJAX call does not complete until after the delay.
Why would this be so? Shouldn't the AJAX functionality complete while
in the delay?

For the delay, I an using a brute force while loop -- so there is no
setInterval problem here.

So, how can I do this sequence?
1 - Call AJAX which sets the hidden variable.
2 - Delay
3 - Get the changed value
Oct 20 '08 #1
6 7921
On 2008-10-20 17:37, sheldonlg wrote:
What I want to do is run an AJAX script that sets a hidden variable on
the form. I call the AJAX script from a javascript function. After the
call, I check the value of that hidden variable and proceed according to
whether it is zero or one.

The problem is that the AJAX call does not complete before the test.
So, I put in a delay after the call to the AJAX function. I even tried
5000 msec. Meanwhile, I looked at the value of the hidden variable
using Firebug. What happens is that the variable does not change until
the delay is completed and the value that the function obtains is the
old value.
Are you sure that you're actually sending an asynchronous request?
What's the third parameter in [XMLHttpRequest].open()?

If you are sending async requests, I don't know why the value should
only change after your setTimeout delay (except maybe if the response
took a very long time).

Anyway, the proper way of initiating an action after an async response
is to use a callback function:

[XMLHttpRequest object].onreadystatechange = function () {
if ([XMLHttpRequest object].readyState == 4) {
myCallbackFunction();
}
};

If that doesn't work, you'll need to show us some code.
- Conrad
Oct 20 '08 #2
Conrad Lender wrote:
On 2008-10-20 17:37, sheldonlg wrote:
>What I want to do is run an AJAX script that sets a hidden variable on
the form. I call the AJAX script from a javascript function. After the
call, I check the value of that hidden variable and proceed according to
whether it is zero or one.

The problem is that the AJAX call does not complete before the test.
So, I put in a delay after the call to the AJAX function. I even tried
5000 msec. Meanwhile, I looked at the value of the hidden variable
using Firebug. What happens is that the variable does not change until
the delay is completed and the value that the function obtains is the
old value.

Are you sure that you're actually sending an asynchronous request?
What's the third parameter in [XMLHttpRequest].open()?

Yes, it is asynchronous.

objRequest.open('POST', pUrl, true);
>
If you are sending async requests, I don't know why the value should
only change after your setTimeout delay (except maybe if the response
took a very long time).
VERY short time.
>
Anyway, the proper way of initiating an action after an async response
is to use a callback function:

[XMLHttpRequest object].onreadystatechange = function () {
if ([XMLHttpRequest object].readyState == 4) {
myCallbackFunction();
}
};
I believe I am doing that -- and it works and is fast.
objRequest.onreadystatechange =
function() { handleResponse(objRequest); };

===========

function handleResponse(pObjRequest) {
if (pObjRequest.readyState==4) {
if (pObjRequest.status==200) {
resp = pObjRequest.responseText;
parseMultiXML(resp);
pObjRequest=null;
}
}
}
If that doesn't work, you'll need to show us some code.
If you wish, I will put up my AJAX black box utility that I put
together. I will also show the piece of code that is giving me
problems. I'll await your response to this post.

>

- Conrad
Oct 21 '08 #3
On Oct 20, 5:37*pm, sheldonlg <sheldonlgwrote:
(..)
The problem is that the AJAX call does not complete before the test.
So, I put in a delay after the call to the AJAX function. *I even tried
5000 msec. *Meanwhile, I looked at the value of the hidden variable
using Firebug. *What happens is that the variable does not change until
the delay is completed and the value that the function obtains is the
old value.

It seems as if the AJAX call does not complete until after the delay.
Why would this be so? *Shouldn't the AJAX functionality complete while
in the delay?
Yes but no. The XHR request completes but no callback can be called
because you've got JS stuck into a loop. The Golden rule is: *never*,
under no circumstances will two things happen at the same time in JS,
because it's single-threaded. (and being single threaded is a Good
Thing, BTW).
For the delay, I an using a brute force while loop -- so there is no
setInterval problem here.
Don't use loops for timing, it's a nonsense. Nothing else, nothing but
the loop can/will happen in the browser during a loop. It's as
freezing the time. Read the FAQ.
So, how can I do this sequence?
1 - Call AJAX which sets the hidden variable.
2 - Delay
3 - Get the changed value
JS is single threaded so nothing else can happen since you are
throwing it into a brute-force while loop...

To check that/if something has happened every once in a while inline
something like:

(function () {
if (hasHappenedAlready) {
doWhatYouWantedToDo();
} else {
//keep waiting
setTimeout(arguments.callee, 100); //check again in 100ms.
}
})();

But you could as well have used a *synchronous* XHR call instead.

--
Jorge.
Oct 21 '08 #4
On 2008-10-21 09:52, Jorge wrote:
On Oct 20, 5:37 pm, sheldonlg <sheldonlgwrote:
>For the delay, I an using a brute force while loop -- so there is no
setInterval problem here.

Don't use loops for timing, it's a nonsense. Nothing else, nothing but
the loop can/will happen in the browser during a loop. It's as
freezing the time. Read the FAQ.
Ah, I missed that part about the while loop. Of course that's the
problem, and yes, it's a very bad idea.
- Conrad
Oct 21 '08 #5
Conrad Lender wrote:
On 2008-10-21 09:52, Jorge wrote:
>On Oct 20, 5:37 pm, sheldonlg <sheldonlgwrote:
>>For the delay, I an using a brute force while loop -- so there is no
setInterval problem here.
Don't use loops for timing, it's a nonsense. Nothing else, nothing but
the loop can/will happen in the browser during a loop. It's as
freezing the time. Read the FAQ.

Ah, I missed that part about the while loop. Of course that's the
problem, and yes, it's a very bad idea.
- Conrad
Thanks everyone.

Changing to setTimeout didn't help any. However, the question about
asynchronous did help. It made me look at my objective. What I
**REALLY** wanted was to have a return value from AJAX and not the
updating of a control. (I am checking to see if the username exists as
part of an overall validation). Therefore, what I really wanted was
synchronous. (I only concerned myself with the delay as a workaround by
getting a value from having set a hidden control in the AJAX call.) I
put in this little function:

function syncAJAX(pURL)
{
var req = getRequestObj();
req.open('GET', pURL, false);
req.send(null);
return req.responseText;
}

and called it instead. The invoked php function returns either 0 or 1.
This worked like a charm.
Oct 21 '08 #6
On Oct 21, 7:10*am, sheldonlg <sheldonlgwrote:
>
and called it instead. *The invoked php function returns either 0 or 1.
* This worked like a charm.
Your users will not be charmed by that. Never send synchronous
requests. Learn to use Ajax properly or don't use it.
Oct 22 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Grant Merwitz | last post by:
Hi I am trying to implement the Microsoft Ajax.NET extensions to perform a lookup on a key press of a text box. What this will do is once a user enters a letter into the textbox, this will...
13
by: Marvin Zhang | last post by:
Hi, I'm not familiar with web programming, but I have a problem here. I have a page. When a user click one button on it, I will use AJAX to request a PHP script which will do a bunch of tasks,...
10
by: shankwheat | last post by:
I'm experimenting with using a AJAX style "processing" icon. The process I'm running in the background with xmlHttp is intensive and takes a 5--10 secs to complete. Instead of my processing icon...
8
by: =?Utf-8?B?SmFrb2IgTGl0aG5lcg==?= | last post by:
I am new to AJAX. I am applying AJAX to a current web solution to get the "instant behaviour". On my main page I have two sets of criteria: Specific and Wide. Each set is placed in a View...
3
by: nghivo | last post by:
I attempted to synchronize async Ajax calls using the following JS blocks: ==================================================== function getXMLHTTPRequest() { try { req =...
3
by: Dica | last post by:
i've got a script that takes about 10 seconds to complete with page_load. for that reason, i turned to ajax for additional dynamic updates i needed to do on the page without having to force the...
3
JamieHowarth0
by: JamieHowarth0 | last post by:
Hi folks, I have a bit of a headache. I've finally added all the nice finishing touches to my own website (static only with a bit of DHTML). Now I've just converted the whole thing to AJAX with a...
1
by: violinandviola | last post by:
I have just put 4 different ajax bits on this page: http://jimpix.co.uk/default-ajax.asp The ajax spits out chunks of images / news content, and users can view the chunks via next / prev links....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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,...

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.