Michael G. & Michael L. …

 

This is interesting…

 

With this code executing:

(This code will seem a little strange since it’s involves more functions

than is absolutely necessary, but it’s part of my attempt to “encapsulate”

some of my code to make it more manageable…some of the sequences

of specific actions occur repeatedly, so I’ve been bundling the specific

jQuery actions into “def” or “definition” functions, then calling the

“grouping” functions with single functions… the code on complex ajax/jQuery

pages quickly get out-of-control on large pages) 

 

success:  function(response) {

                        if         ( response.MESSAGE == ‘Success’)

 

                                   { gNewStoryTITLE = response.TITLE;

                                      console.log(“a”, gNewStoryTITLE);

                                      defCreateStorySuccessResponse();

                                      console.log(“b”, gNewStoryTITLE);  }

 

                      else     { alert(‘Rats! It didn\’t work!’); }

                  }

 

from there is goes to:

 

function defCreateStorySuccessResponse() {

 

     $(‘#addFirstSectionUL:visible’).hide();

     $(‘#addStoryTitleSuccessResponseUL:hidden’).fadeIn(500);

     console.log(“c”, gNewStoryTITLE);

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

     console.log(“d”, gNewStoryTITLE);

 

}

 

Now, when those code is run, firebug responds with:

 

1)       gNewStoryTITLE is not defined

       console.log(“c”, gNewStoryTITLE);

 

2)      a Story Title Number 33   <--- (“Story Title Number 33” is the Story 
Title variable)

3)      c Story Title Number 33

4)      d Story Title Number 33

5)      b Story Title Number 33

 

Notice that the console.log outputs are appearing out of sequence: a, c, d, b

 

What accounts for that?  And how can I force proper sequencing of execution?

 

Thanks,

 

Rick

 

 

 

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

 

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.' 

Inactive hide details for "Rick Faircloth" ---04/06/2009 02:08:03 PM---This is 
part of a content management system for a client"Rick Faircloth" ---04/06/2009 
02:08:03 PM---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 pr



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



<<image001.gif>>

<<image003.png>>

<<image004.png>>

Reply via email to