The function should occur sequentially, but that execution of your success
function might not.  AJAX is by default asynchronous so it will not affect
the rest of the page loading.  That also means that if you try to access a
variable that is only set by that success function before AJAX returns, you
might get that error.  Do you have an example of where else in your code
that variable is being accessed?

cheers

Michael Lawson
Content Tools Developer, Global Solutions, ibm.com
Phone:  1-828-355-5544
E-mail:  mjlaw...@us.ibm.com

'Examine my teachings critically, as a gold assayer would test gold. If you
find they make sense, conform to your experience, and don't harm yourself
or others, only then should you accept them.'


                                                                                
                                                  
  From:       "Rick Faircloth" <r...@whitestonemedia.com>                       
                                                  
                                                                                
                                                  
  To:         <jquery-en@googlegroups.com>                                      
                                                  
                                                                                
                                                  
  Date:       04/06/2009 02:08 PM                                               
                                                  
                                                                                
                                                  
  Subject:    [jQuery] Re: Trying to understand how to use global variables...  
                                                  
                                                                                
                                                  





This is part of a content management system for a client, so it’s not.
publicly accessible…if it appears I can’t solve the problem otherwise,
I’ll set up a test case based on the code that is publicly accessible.

However, here’s the relevant code from the app that shows what I’m
doing:  (and just so you’re not confused, I’ve swapped from using
the gNewStoryID variable to gNewStoryTITLE variable, but the usage
is exactly the same)

From the AJAX success section in which the global variable is set:

success:  function(response) {
                        if (response.MESSAGE == ‘Success’ )
                            {gNewStoryTITLE = response.TITLE;
                              alert(gNewStoryTITLE);    <--- variable shows
up here with correct value
                              fnCreateStorySuccessResponse();}

So you can see that the fn in which I’m going to use the new global
variable
is being called *after* the global var is established, in
fnCreateStorySuccessResponse().

fnCreateStorySuccessResponse consists of this code:
(it has more lines than below, but they are irrelevant and don’t involve
the variable)

$(‘#newStoryTitleSPAN’).empty().append(gNewStoryTITLE);

The gNewStoryTitle variable shows up in the specified Span correctly, but
then firebug
immediately throws and “gNewStoryTITLE is not defined” error.

Actually, from observing the sequence of events on the screen, even after
removing the
alert(gNewStoryTITLE); from the sequence, it almost appears as if the
sequence of events is firing out of sequence that is specified above.

The error message is actually showing up in firebug before the
gNewStoryTitle value
gets displayed on-screen in the span, which happens when
fnCreateStorySuccessResponse()
is triggered.

It’s as if fnCreateStorySuccessResponse() tries to use the variable before
it is set, then
after the error message is displayed, the variable is finally set and
properly displayed on-screen.  But that doesn’t make sense.

Shouldn’t the order of statements in the success: section of the ajax
fuction occur
sequentially?

Rick

From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On
Behalf Of Michael Geary
Sent: Monday, April 06, 2009 1:37 PM
To: jquery-en@googlegroups.com
Subject: [jQuery] Re: Trying to understand how to use global variables...

It would be easier if you had a link to a test page or at least some code
to look at. Otherwise we can only guess what might be wrong.

Here is one possibility.

Note what you said here:

> ...by using gNewStoryID = response.STORY_ID, I was creating a
> global variable that could then be used in any function.

(emphasis added)

You are exactly right: First you create the global variable with the
assignment statement, and then you can use it.

The question is, when is then?

The global variable is created when your callback function executes.

These other functions where you're trying to use it: Are they being called
before the callback function executes, or after? If you put alert() or
console.log() calls wherever you set or reference your global variable,
what order do they appear in?

-Mike


 From: Rick Faircloth
 I’ve read up on the subject, but my application is showing that
 my understanding has gaps.

 If I have an ajax function that in the returned “response” exists
 a value called “response.STORY_ID” and I used the statement in the
 success section of the ajax function, gNewStoryID = response.STORY_ID,
 and then alert(gNewStoryID), I do get the property ID returned in the
 alert.

 Now I want to use that variable, gNewStoryID, in other functions.  My
 understanding
 was that, by using gNewStoryID = response.STORY_ID, I was creating a
 global variable
 that could then be used in any function.

 However, when I try to use the gNewStoryID in another function, for
 instance,
 $(‘#newStoryIDSpan’).empty().append(gNewStoryID);
 I get an error stating “gNewstoryID is not defined”.

 Where is my understanding in complete?  I was think that a global
 javascript variable
 would be available through the application as is a ColdFusion session
 variable.

 Thanks for helping me understand!

 Rick
 
---------------------------------------------------------------------------------------------------------------------------------------
 "It has been my experience that most bad government is the result of too
 much government." - Thomas Jefferson

<<inline: graycol.gif>>

<<inline: ecblank.gif>>

Reply via email to