By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

AJAX + delay

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.