473,394 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

JS/Ajax Woes

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 1641
pbmods
5,821 Expert 4TB
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
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 Expert 4TB
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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,...
0
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...
0
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...

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.