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 7906231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:50:32+00:00 2026-06-03T10:50:32+00:00

Possible Duplicate: jQuery: Return data after ajax call success I have the following function

  • 0

Possible Duplicate:
jQuery: Return data after ajax call success

I have the following function that is called from another function. It needs to return something in order to see whether the function worked or not.

//Function to close any existing connection 
function closeConnection(manual) {
    //Establish connection to php script 
    $.ajax({
        type: 'POST',
        url: 'action/chat/closeconnection.php',
        success: function (feedback) {
            //If this function was called manually, also empty chatTextDiv and fade out chatDiv
            if (manual == true) {
                //Stop getting messages
                clearInterval(setintervalid);
                //Fade out chatDiv
                $('#chatDiv').fadeOut(100);
                //Empty chatTextDiv
                $('#chatTextDiv').html('');
                //Fade in openChatDiv
                $('#openChatDiv').fadeIn(100);
            }
        }
    }).error(function () {
        //Append with connection error - user doesn't know that he is disconnected first so it is better to say this as the function will return false and stop other functions
        if (manual != true) {
            $('#chatTextDiv').append('You could not be connected. Please try again later.<hr/>');
            //Stop getting messages
            clearInterval(setintervalid);
            var closeconnectionsuccess = false;
        }
        elseif(manual == true) {
            $('#chatTextDiv').append('You could not be disconnected. Please try again later.<hr/>');
            var closeconnectionsuccess = true;
        }
    });

    return closeconnectionsuccess;
}

It is really only about the last line where I want to return the success of the function: The code doesn’t work then. Why not?

  • 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-03T10:50:33+00:00Added an answer on June 3, 2026 at 10:50 am

    As @Rockitsauce said, your closeconnectionsuccess variable is not in the scope of the function and therefore not accessible at that point. More importantly, however, you have to take into account that you are using an asynchronous function of jQuery – the function closeConnection will complete execution before the callbacks (success, error) are called. You’ll have to add a callback function to your own function as well and thereby make it asynchronous, too, if you want to retrieve any result after the HTTP request has completed.

    This code should therefore work:

    //Function to close any existing connection 
    function closeConnection(manual, callback) {
    //Establish connection to php script 
    $.ajax({
        type: 'POST',
        url: 'action/chat/closeconnection.php',
        success: function(feedback) {
            //If this function was called manually, also empty chatTextDiv and fade out chatDiv
            if(manual==true)
            {
                //Stop getting messages
                clearInterval(setintervalid);
                //Fade out chatDiv
                $('#chatDiv').fadeOut(100);
                //Empty chatTextDiv
                $('#chatTextDiv').html('');
                //Fade in openChatDiv
                $('#openChatDiv').fadeIn(100);
            }
        }
    }).error(function() {
        //Append with connection error - user doesn't know that he is disconnected first so it is better to say this as the function will return false and stop other functions
        if(manual!=true)
        {
            $('#chatTextDiv').append('You could not be connected. Please try again later.<hr/>');
            //Stop getting messages
            clearInterval(setintervalid);
            callback(false);
        }
        elseif(manual==true)
        {
            $('#chatTextDiv').append('You could not be disconnected. Please try again later.<hr/>');
            callback(true);
        }
    });
    }
    
    closeConnection(manual, function(success) {
        //Process result
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: jQuery: Return data after ajax call success I wrote a script which
Possible Duplicate: How do you know when to return false from a jQuery function?
Possible Duplicate: JQuery ajax cross domain I need to load content from another websites
Possible Duplicate: Prevent click event from affecting parent jquery I have a DOM structure
Possible Duplicate: jQuery delay between animations I have created two functions: One that animates
Possible Duplicate: jquery ajax call taking too long or something for some reason this
Possible Duplicate: jQuery: get the file name selected from <input type=file /> I have
Possible Duplicate: AJAX- response data not saved to global scope? I basically have a
Possible Duplicate: jQuery ajax error function I am trying to replace a global ajax
Possible Duplicate: jQuery 1.7 - Turning live() into on() Just switching my code from

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.