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 6 7894
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
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
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.
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
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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,...
|
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...
|
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...
|
by: nghivo |
last post by:
I attempted to synchronize async Ajax calls using the following JS blocks:
====================================================
function getXMLHTTPRequest()
{
try
{
req =...
|
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...
|
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...
|
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....
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |