473,513 Members | 2,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JS/Ajax Woes

2 New Member
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!

Expand|Select|Wrap|Line Numbers
  1. function get_calendar_events(day, month, year)
  2. {
  3.      // arbitrarily set to 10 while debugging this script
  4.      var num_events = 10;
  5.      var calendar_ajax = new_ajax_object();
  6.      if( calendar_ajax )
  7.      {
  8.           calendar_ajax.onreadystatechange = function(){
  9.                if( calendar_ajax.readyState == 4 )
  10.                {
  11.                     // should be set to 0 for all dates right now
  12.                     num_events = 0;
  13.                     // alert(num_events);
  14.                }
  15.           }
  16.           var URL = '';
  17.           calendar_ajax.open('GET', URL, true);
  18.           calendar_ajax.send(null);
  19.      }
  20.      else
  21.      {
  22.           alert("Unable to retrieve calendar data.");
  23.      }
  24.      // num_events = 10 (but should be 0!!)
  25.      return num_events;
  26. }
  27.  
Jun 23 '07 #1
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:

Expand|Select|Wrap|Line Numbers
  1. function get_calendar_events(day, month, year, callback) {
  2. .
  3. .
  4. .
  5.  if( calendar_ajax )
  6.      {
  7.           calendar_ajax.onreadystatechange = function() {
  8.                if( calendar_ajax.readyState == 4 )
  9.                {
  10.                     // should be set to 0 for all dates right now
  11.                     num_events = 0;
  12.                     // alert(num_events);
  13.  
  14.                     if(callback && (callback.constructor == Function))
  15.                         callback(num_events);
  16.                }
  17.           }
  18.           var URL = '';
  19.           calendar_ajax.open('GET', URL, true);
  20.           calendar_ajax.send(null);
  21.      }
  22. .
  23. .
  24. .
  25. }
  26.  
  27. get_calendar_events(month, day, year, function(eventCount) {
  28.     alert(eventCount);
  29. });    // Alerts '0' after a slight delay.
  30.  
Jun 23 '07 #2
permafrost91
2 New Member
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?
Jun 24 '07 #3
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:
Expand|Select|Wrap|Line Numbers
  1. var id = 6, name = 'Events';
  2. call_some_other_function(id, name, get_calendar_events);
Try doing this instead:
Expand|Select|Wrap|Line Numbers
  1. var id = 6, name = 'Events';
  2. call_some_other_function(id, name, function(day, month, year) {
  3.     get_calendar_events(day, month, year, function() {
  4.         alert(num_events);
  5.     });
  6. });
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.
Jun 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
1705
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...
0
1501
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...
3
3440
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...
2
1818
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...
0
2070
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. ...
4
4291
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...
4
1975
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...
1
16474
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 -...
5
2435
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...
2
5521
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...
0
7260
marktang
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,...
1
7101
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...
0
7527
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...
0
5686
agi2029
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,...
1
5090
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...
0
3234
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...
0
3223
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1597
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 ...
1
803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.