PDA

View Full Version : JS/Ajax Woes



permafrost91
06-23-2007, 02:08 PM
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;
}