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 1641
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?
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: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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: 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...
|
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...
| |