You've got the "x" set inside the callback and are trying to alert it
*before* it gets back from it the $.post call, hence it's empty

so if you have

$.post( ... do ajax stuff to variable "x" ..... )
alert(x);  // <---  this gets run right away, the asynchronous call of
$.post  doesn't "wait to finish" before going to the next line

so another way of looking at it:

var foo = "1";
$.post(  some ajax call that takes 2 seconds to set foo to "99"  );
alert(foo);

that is going to alert "1" each and every time, because JavaScript
isn't going to "wait" before doing the alert

does that help?




On Oct 9, 12:18 pm, Adonis <achrysoch...@hotmail.com> wrote:
> ok, here is an effort to sum up the code..
>
> *
> function initialiseLegendLayerGroupEntry(a,b,c,d,e,f) {
> var x;
>     $("#division_name").bind("click", function() {
>         if(this.value == "sth") {
>             y=callFunction(a,b,c,d,e,f);
>             $.ajax({
>                 type: "POST",
>                 url: "/serverFunction/",
>                 data: {project_name:d,layer_name:e,group_name:f},
>                 success: function(msg){
>                     $("#an entry point for html files that are
> served").after(msg);
>                     alert( $("#division where the wanted value is
> stored").val() );  // this alert window pops up at the end.
>                     x= $("#division where the wanted value is
> stored").val();
>                      }
>             });
>     alert(x); // this pop up window is always empty. it opens while i
> still receive "print" commands from my server script !
>         }
>     });
>
> }
>
> I think this code is a good example of my problem. Can you identify an
> error somewhere?
>
> thanks,
>
> On Oct 8, 6:07 pm, MorningZ <morni...@gmail.com> wrote:
>
> > You should should the specific $.post() code in detail, there's
> > something you are missing.... because when you get back from the async
> > call, everything (global variables, page objects) are fully available
> > at that point... there's zero need to use setTimeout
>
> > On Oct 8, 12:27 pm, Adonis <achrysoch...@hotmail.com> wrote:
>
> > > Dan,
>
> > > I used a $.ajax() call with a callback -> success: function(msg) etc.
> > > but still it does not work the way I want to.
> > > from the server side i am using,
>
> > > *
> > > return render_to_response("blah.html",{ 'project_id':project_id },
> > > context_instance = RequestContext(request))
> > > *
> > > putting the project_id in a <div><input id="klain" value=project_id /
>
> > > ></div>
>
> > > and if the ajax call is successfull i do this,
>
> > > *
> > > alert( $(#klain).val() );
> > > *
>
> > > The alert window does pop up, but after the rest of the functions in
> > > the code have been read. I somehow need to delay the javascript
> > > parsing, until the input of the division in the html that is rendered
> > > gets the "project_id" value. This is why i am looking into setTimeOut
> > > () js function.
>
> > > cheers,
>
> > > On Oct 8, 2:08 pm, "Dan G. Switzer, II" <dswit...@pengoworks.com>
> > > wrote:
>
> > > > Adonis,
> > > > The $.post() function is asynchronous (by default) when your code hits a
> > > > $.post() call, it'll keep executing code. In order to do something with 
> > > > the
> > > > results of your AJAX call, you'll want to use the callback 
> > > > option--which is
> > > > a function run when the AJAX call is completed.
>
> > > > -Dan
>
> > > > On Thu, Oct 8, 2009 at 7:14 AM, Adonis <achrysoch...@hotmail.com> wrote:
>
> > > > > I am not entirely sure if i am using proper definitions here, but here
> > > > > it goes..
>
> > > > > I have $.post() inside javascript functions. If ajax is successfull,
> > > > > it returns data from the server. I am trying to assign this data  to
> > > > > javascript global variables. The problem is that my global javascript
> > > > > variables end up to be empty. It seems it takes time for the ajax to
> > > > > return the data and by the time the server data is returned, the
> > > > > browser has comploted reading through the rest of the javascript
> > > > > functions. Thus, leaving my global variables empty.
>
> > > > > Do you have any suggestions on how this could be fixed? I am trying
> > > > > setTimeOut() at the moment but i would be glad to concider other
> > > > > solutions as well..
>
> > > > > I could provide code examples on request.
>
> > > > > Thanks in advance!

Reply via email to