473,785 Members | 2,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7946
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].onreadystatech ange = function () {
if ([XMLHttpRequest object].readyState == 4) {
myCallbackFunct ion();
}
};

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].onreadystatech ange = function () {
if ([XMLHttpRequest object].readyState == 4) {
myCallbackFunct ion();
}
};
I believe I am doing that -- and it works and is fast.
objRequest.onre adystatechange =
function() { handleResponse( objRequest); };

===========

function handleResponse( pObjRequest) {
if (pObjRequest.re adyState==4) {
if (pObjRequest.st atus==200) {
resp = pObjRequest.res ponseText;
parseMultiXML(r esp);
pObjRequest=nul l;
}
}
}
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 (hasHappenedAlr eady) {
doWhatYouWanted ToDo();
} else {
//keep waiting
setTimeout(argu ments.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.responseTex t;
}

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
8738
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 filter a list from a database and populate some filtered options for the user to choose in a seperate listbox. Any subsequent key presses will further filter this result set.
13
4006
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, asynchronously. These tasks might take long time so I want to keep the user informed of the progress. The problem is that only the PHP script knows the progress, how can the web page gets these information from PHP script?
10
8011
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 appearing in the page, the page just freezes during the process until it's finished. Is this the best way to display this kind of icon? What can I do so this works right? Thanks function stateChanged() { if (xmlHttp.readyState == 0)
8
2582
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 container. I am not sure that I configured the Triggers correct, but the behaviour is OK. When I click the linkbuttons they switch between two sets of search controls. When I click the search buttons they reload the display panel. BUT .... if the...
3
2758
by: nghivo | last post by:
I attempted to synchronize async Ajax calls using the following JS blocks: ==================================================== function getXMLHTTPRequest() { try { req = window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP"); } catch (err) { } return req;
3
1837
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 user through that 10 second delay, but it looks like page_load still gets called with an ajax submit. am i doing something wrong with my ajax calls or is this normal? tks
3
5478
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 couple of simple functions and links changed here and there. However, I want to create a really cool effect with my AJAX - which is I want to induce a "delay" so people actually see the "loading" section, cause at the moment, it's doing it at about...
1
1850
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. When I first view the page in Firefox, there is a short delay while the content first loads in the ajax sections. When I first view the page in IE6, none of the ajax content appears to
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10151
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
10092
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
9950
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...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.