The function below should return (to another function drawing the calendar) how many events are in the calendar for the given date. Right now, I have the problem that num_events does not get changed inside the onreadystatechange function(). I arbitrarily initialized num_events to 10 and said to set num_events to 0 inside function() but get_calendar_events() always returns 10. If you uncomment the alert() inside the function(), it displays "0". Nevermind the URL, I left that out because it's too long and that part of the script works (I can alert(calendar_ajax.responseText) and it shows the correct number of events).
Where am I going wrong here? Why can I not reassign num_events from inside function()?
I also tried defining num_events outside get_calendar_events() to make it global but function() still can't change its value.
Thank you for your help, it is greatly appreciated! -
function get_calendar_events(day, month, year)
-
{
-
// arbitrarily set to 10 while debugging this script
-
var num_events = 10;
-
var calendar_ajax = new_ajax_object();
-
if( calendar_ajax )
-
{
-
calendar_ajax.onreadystatechange = function(){
-
if( calendar_ajax.readyState == 4 )
-
{
-
// should be set to 0 for all dates right now
-
num_events = 0;
-
// alert(num_events);
-
}
-
}
-
var URL = '';
-
calendar_ajax.open('GET', URL, true);
-
calendar_ajax.send(null);
-
}
-
else
-
{
-
alert("Unable to retrieve calendar data.");
-
}
-
// num_events = 10 (but should be 0!!)
-
return num_events;
-
}
-
3 1650 pbmods 5,821
Recognized Expert Expert
Heya, permafrost91. Welcome to TSDN!
The first 'A' in AJAX stands for 'Asynchronous', which means that your script won't wait for the AJAX request to return before going on to the next statement in your code.
Your code will send off the AJAX request, and then immediately go on to return num_events, which is still 10 at that point.
If you were to try to access the value of num_events, say, 10 seconds later, the value should be set back to 0.
The only reliable solution would be to force your script to 'wait' for the AJAX call to return. The easiest way to do that would be to append the code you want to execute as a callback function: -
function get_calendar_events(day, month, year, callback) {
-
.
-
.
-
.
-
if( calendar_ajax )
-
{
-
calendar_ajax.onreadystatechange = function() {
-
if( calendar_ajax.readyState == 4 )
-
{
-
// should be set to 0 for all dates right now
-
num_events = 0;
-
// alert(num_events);
-
-
if(callback && (callback.constructor == Function))
-
callback(num_events);
-
}
-
}
-
var URL = '';
-
calendar_ajax.open('GET', URL, true);
-
calendar_ajax.send(null);
-
}
-
.
-
.
-
.
-
}
-
-
get_calendar_events(month, day, year, function(eventCount) {
-
alert(eventCount);
-
}); // Alerts '0' after a slight delay.
-
Thanks for your help! The only problem is that the current design of the site won't allow this: get_calendar_events() is already the callback function. Any other ideas?
pbmods 5,821
Recognized Expert Expert
Heya, permafrost91.
Hm. That's going to be tricky. Adding a callback to get_calendar_events is probably the only really good way to do it (note that with the code I posted, you could also *not* specify a value for callback, and you would not get an error).
If you're calling get_calendar_events as a callback function already, you could instead pass an anonymous function.
As an example, if you are already doing this: - var id = 6, name = 'Events';
-
call_some_other_function(id, name, get_calendar_events);
Try doing this instead: - var id = 6, name = 'Events';
-
call_some_other_function(id, name, function(day, month, year) {
-
get_calendar_events(day, month, year, function() {
-
alert(num_events);
-
});
-
});
In this case, instead of passing get_calendar_events as the callback, you're passing an anonymous function that calls get_calendar_events with the extra callback argument.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mark |
last post by:
O, woe is me, to have seen what I have seen, see what I see!
(That's Shakespeare for those who were wondering what I'm on about)
I am "having fun" with cookies.
And I wonder if I have...
|
by: Cedric |
last post by:
This is a 3 weeks old problem, but having found a solution (and having
looked for one here, finding only this message), I'm replying now.
From: Jive (someone@microsoft.com)
Subject: Upgrade...
|
by: Angel Cat |
last post by:
Trying to get my jobs to send mail when job fails. Should be easy but
it's giving me headache
Had a whole slew of issues. Outlook is installed with a n outlook
mail profile set up that can...
|
by: Andrew Thompson |
last post by:
- NN 4.78 rendering woes, links at far left -
I am trying to rework an old site, make it valid html
and css, improving the x-browser and 'older browser'
compatibility. My efforts so far, have...
|
by: Arun Bhalla |
last post by:
I'm having some inconsistency problems with my deployment project ("Setup")
and its custom actions ("Installer"). I'm using Visual Studio .NET 2003
(.NET 1.1, no service pack) on Windows XPSP1.
...
| |
by: bobzimuta |
last post by:
I'm creating a simple AJAX library. It's an object that will return an
array containing the response text or xml. I'm trying to find a way to
assign the response as a property of the object, but...
|
by: petermichaux |
last post by:
Hi,
If I type http://domain.com/product/5 into my browser I will see the
product details in the main div plus a side bar div containing links to
other products. This side bar is common to all...
|
by: www.web20developers.com |
last post by:
http://www.web20developers.com
http://www.web20developers.com/index.php?option=com_content&task=view...
Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell
AJAX -...
|
by: salvador |
last post by:
I'm trying to create a function to return some values from a php
script. The php script is returning the correct values if called from
a browser window. However, the function that I'm using never...
|
by: =?Utf-8?B?REo=?= |
last post by:
I have a peculiar problem here that I did not have until I migrated from
ASP.NET 2.0 to 3.5.
I use a master page for my application. Because the master page uses update
panels I have the...
|
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,...
| |
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...
|
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...
|
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: 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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |