Looks like we have two threads going on the same topic. :-)

To give you a real code example, I'd need to see the code that makes use of
that 'result' variable you're setting. Where is that code and what does it
look like?

The bottom line is simple: whatever the code is that uses the 'result'
variable, that code needs to be made into a function, and you need to call
that function in the ajax success callback. Or, that function can *be* the
success callback.

You can't just set a variable in an asynchronous callback and expect other
code somewhere else on the page to somehow "know" when your variable is
ready for use.

It's not a question of "how far deep in nested functions can you return a
value". Any nested function can return a value; it doesn't matter how far
deeply your functions are nested. But that return value goes only to
*whoever called that function*. In the case of a $.ajax callback, the return
value is being passed back into the $.ajax code, which probably ignores the
value completely.

The real issue is *when* the code is run.

Can you post a URL to a complete test page? That would make it a lot easier
to suggest the right way to write your code.

-Mike

On Mon, Dec 14, 2009 at 5:48 AM, Jojje <jojjsus_chr...@hotmail.com> wrote:

> Hi! Yeah i got that tip before but i'm not sure i understand this.
> Could you perhaps give me an example with the code below :)
>
> regards
>
> George
>
> On 14 Dec, 14:27, Rick van Hoeij <rickvho...@gmail.com> wrote:
> > because of the async of javascript I normally use callback functions
> > to do the value passing. So instead of return true or return false in
> > a ajax function, it's better to call a function with the true or false
> > as a parameter.
> >
> > Just my two cents ;)
> >
> > On 12 dec, 20:24, Jojje <jojjsus_chr...@hotmail.com> wrote:
> >
> > > How deep in the scope can you go in nested functions and still return
> > > a value? For example:
> >
> > > $.validator.addMethod('userCheck', function (value) {
> > >     $.ajax({
> > >         type: "POST",
> > >         url: "_scripts/send_message.php",
> > >         data: "action=checkuser& username=" + value,
> > >         success: function(msg) {
> > >             if (msg) {
> > >                 return false;
> > >             }
> > >             else {
> > >                 return true;
> > >             }
> > >         }
> > >     });
> >
> > > },"");
> >
> > > Why is´nt this working?
> >
> > > If i do this:
> >
> > > //Global
> > > var result;
> >
> > > $.validator.addMethod('userCheck', function (value) {
> > >     $.ajax({
> > >         type: "POST",
> > >         url: "_scripts/send_message.php",
> > >         data: "action=checkuser& username=" + value,
> > >         success: function(msg) {
> > >             if (msg) {
> > >                 result = false;
> > >             }
> > >             else {
> > >                 result = true;
> > >             }
> > >         }
> > >     });
> > >     return result;
> >
> > > },"");
> >
> > > i get the value back but it does not work properly, It gets the value
> > > from the textfield but on keyup it's always one letter short... so i
> > > have to type one more letter to get the right value. I´m stuck on this
> > > so any help would be greatly appreciated! :)
> >
> > > Thanks in advance
> >
> > > George
>

Reply via email to