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

  • Home
  • SEARCH
  • 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 7886637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:22:10+00:00 2026-06-03T05:22:10+00:00

I have two questions regarding the following function. Obviously, it is about a chat.

  • 0

I have two questions regarding the following function. Obviously, it is about a chat. In the function chat() different functions are called, one to establish a connection, one to search for someone to chat with (randomly), and one to get messages every second.

function chat()
{
    //Open connection
    var openconnection=openConnection();
    //Stop function if connection could not be established
    if(openconnection==false) return;

    //Search for someone to chat with
    searchforcontact=searchForContact();
    if(searchforcontact==false) return;

    //Constantly get messages
    setTimeout(function(){
        setInterval(getMessages(),1000);
    },2000);
}

function openConnection() { 
    //Establish connection to php script 
    $.ajax({ 
        type: 'POST', 
        url: 'action/chat/openconnection.php', 
        success: function(connection) { 
            //Let user know that someone to chat with is searched for 
            $('#chatTextDiv').append('bla'); 
            //Return that the connection was successfull 
            return true; 
        } 
    }).error(function() { 
        //Let user know that a connection could not be established 
        $('#chatTextDiv').append('bla'); 
        //Return false 
        return false; 
    }); 
}

Here are my questions:

1: I use return to stop the function chat() if, e.g., a connection could not be established. However the functions goes on to searchForContact(), and even if that fails, still goes on. How come?

2: The function getMessages() only runs once, I wonder why? FYI, I use the timeout for usability.

  • 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-03T05:22:11+00:00Added an answer on June 3, 2026 at 5:22 am

    Most likely openConnection() doesn’t return false. Since synchronous API are very uncommon and not really useable from within JavaScript, I am pretty sure, that openConnection does not work the way you use it. Please provide more information on the openConnection function.

    Also, instead of passing the getMessages function to the call to setInterval, you invoke getMessages and pass whatever it returns to setInterval. This is most likely not what you want. You should change that call to the following:

    setTimeout(function(){
        setInterval(getMessages,1000);
    },2000);
    

    You should really read up on how AJAX and asynchronous APIs in general work. To give you a head-start here is an update to your code that should demonstrate what you’re doing wrong:

    function openConnection(success, error) { 
        //Establish connection to php script 
        $.ajax({ 
            type: 'POST', 
            url: 'action/chat/openconnection.php', 
            success: success
        }).error(error||function () {}); 
    }
    
    function chat(ready) {
        //Open connection
        openConnection(function () {
            // If this function is called, then the connection is established
    
            //Search for someone to chat with
            searchforcontact=searchForContact();
            if(searchforcontact==false) return;
    
            // I STRONGLY SUPPOSE searchForContact is also an asynchronous API!
    
            //Constantly get messages
            setTimeout(function(){
                setInterval(getMessages,1000);
                ready();
            },2000);
        }, function () {
            // when this function is called, something went wrong.
        });
    }
    
    chat(function () {
        // when this function is called, chat is ready to be used!
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two questions regarding array: First one is regarding following code: int a[30];
I have two questions regarding client-side validation in ASP, relating to the following setup:
The following code should be self explanatory. I have two questions regarding the used
I actually have two questions regarding the same problem but I think it is
I actually have two questions regarding exception/error handling in the iPhone app that I
I have two questions regarding the multidimensional arrays. I declared a 3D array using
i am using wireshark Version 1.6.4. i have following questions regarding the stream numbers
I am very new to these issues, please help. I have two questions regarding
I have two questions regarding RewriteRule, but they're both closely related so I hope
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record

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.