-2

My function fetches if the user is banned and i want to return a variable that determines if a popup displays; but as I've found out you can't return a variable from a getJSON function.

 function fetchban() {
    $.getJSON('/fetchban.php',function(data) {

        if(data==0) {

            var banned =  data;
        } else {
            $.each(data,function(index,result) {
            $('#ban-prompt').html(result);
            $('.popup-background').show();
            $('#ban-container-2').show();
            });
        }

    });

    return banned;
}

$('.button').click(function() {
var banned = fetchban();

if(banned==0) {

//display-popup

}

});

There are alot of lines i call the fetchban function, so i would prefer the getJSON in a function. What is the solution?

4
  • then why u using getJSON? use .get() Commented Jul 20, 2012 at 12:08
  • possible duplicate of jQuery getJSON - Return value to the caller function Commented Jul 20, 2012 at 12:10
  • or just set the banned flag inside the JSON, like {banned:"false",datas:{... your regular object}} and then go var banned = data.banned Commented Jul 20, 2012 at 12:10
  • 1
    That's one of the most duplicated question on SO. Just have to look at the right column to see some of the usual answers. Commented Jul 20, 2012 at 12:10

1 Answer 1

3

There are two problems with your function.

The first, and the most significant, is that it assumes that getJSON is synchronous. It is asynchronous by default, meaning it's impossible for fetchban to return a value based on the data retrieved. Instead, fetchban should accept a callback it calls with the result.

The second (which will go away anyway once you fix the first), is that you're trying to return a variable from fetchban that isn't declared (because you declare it inside your getJSON success handler).

So you'd change fetchban to look something like:

function fetchban(callback) {
    $.getJSON('/fetchban.php',function(data) {
        var banned;

        // ...figure out what `banned` should be from the `data`,
        // I'm afraid I couldn't make sense of the code, looked
        // like it always set `banned` to 0.

        // Call the callback with the result
        callback(banned);
    });
}

And then instead of:

$('.button').click(function() {
    var banned = fetchban();

    if(banned==0) {
        //display-popup
    }
});

do this:

$('.button').click(function() {
    fetchban(function(banned) {
        if(banned==0) {
            //display-popup
        }
    });
});

...or much better, fetch it earlier so you already have that information when the click arrives. Users don't like delays when they click, and even the best case on a web round-trip is noticeable to human beings.

Sign up to request clarification or add additional context in comments.

2 Comments

How do i do "fetchban should accept a callback it calls with the result"?
@user892134: See update. Or any of about 757,521 callback tutorials and examples on the web. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.