Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8255539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:33:41+00:00 2026-06-08T01:33:41+00:00

My function fetches if the user is banned and i want to return a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:33:42+00:00Added an answer on June 8, 2026 at 1:33 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pdo function that fetches usernames and user ids from the database.
I have a function in C# that fetches the status of Internet by retrieving
I have the following function which fetches more comments beyond the 20 that are
I am creating a user information edit dialog that fetches edit-user information using $.post
Ok I am making a program in dart that fetches the user location and
as the question states. I have implemented a function wherein it fetches multiple rows
$.get($(this).attr('id'), function(data){ var qp_post = $(data).filter('title'); alert(qp_post); }); This fetches the content fine however
function checkuser(user) { var ret = false; $.ajax({ type:'POST', url:'user.php', async:false, data:{'user':user}, success:function (data)
So I have this function that gets data from database and echoes it. The
I have one go script that fetches any other script requested and this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.