Actually the scope rules I described always apply to all JavaScript code,
whether it uses jQuery or otherwise.

They also apply to your $.ajax example. Consider your second version. You
have a result variable declared outside your validator code. The success
function is able to access this variable even though it's nested two
functions deep (you're inside both the anonymous function passed into
addMethod and the innermost success callback).

This would still work even if the entire example were nested inside yet
another function, so 'result' would now be a local variable in that outer
function.

Given all that, why doesn't your first version work? Well, the $.ajax
function is *asynchronous*. The success callback isn't called right away.
It's called later, after the download has completed. If the success callback
returns a value, where could that value go? It can't be passed through to
become the return value from the $.ajax call, because that function has
*already returned*.

But the second version isn't right either. The success callback will set the
'result' variable as expected, but there's a little problem... How does the
rest of your code know when that variable is ready to look at?

I suppose you could write a polling loop somewhere:

var watchdog = setInterval( function() {
    if( result !== undefined ) {
        clearInterval( watchdog );
        // now result is ready, use it here
    }
}, 100 );

That code checks the 'result' variable 10 times a second, and when it sees
that the variable's value is no longer 'undefined' it stops the interval
timer and uses the variable.

But that would be kind of silly, not to mention inefficient.

Fortunately, there is a much simpler solution. In your success callback,
*call a function* and pass it your ajax result. In fact, that's the reason
why there is a success callback in the first place. $.ajax calls your
success callback when the ajax data has been downloaded and is ready for
use. So any code that uses that data should also be called from your
callback function.

-Mike

On Sun, Dec 13, 2009 at 8:23 AM, Jojje <jojjsus_chr...@hotmail.com> wrote:

> Hahaha! You dont have to apologise for that, my mother should
> apologise to me hahahaha
>
> Ok thank you for your very good and easy explanations on this.
> This was actually part of another qustion i posted here, these rules
> doesnt apply in this example right?
>
> $.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;
>            }
>        }
>    });
>
> },"");
>
> Even though it¨s nested functions i cant return anything outside this
> without using a global variable? Like this:
>
> 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;
>
> },"");
>
>
> regards
>
> George
>

Reply via email to