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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:49:31+00:00 2026-06-17T18:49:31+00:00

I’m making a conversation system where 2 people can chat with each other. I’ve

  • 0

I’m making a conversation system where 2 people can chat with each other. I’ve made an AJAX function which updates the DIV box containing the messages every 2 seconds.

This is working as intended, after a user have written a message. Why isn’t the AJAX call being run right away?

// SET AUTORUN updateMessages() EVERY 2 SECONDS
$(document).ready(function() {
    var interval
    window.onload = function(){
        interval = setInterval('updateMessages()', 2000);
    };
});

// UPDATE #mail_container_conversation
function updateMessages() {
    $.ajax({
        type: "POST",
        url: "<?php echo site_url(); ?>mail/ajaxupdate/<?php echo $user; ?>",
        data: dataString,
         
        success: function(data){
            $("#mail_container_conversation").html(data);
        }        
    });
}


// SEND NEW MESSAGE
$(function(){
    $("#mail_send").submit(function(){
        dataString = $("#mail_send").serialize();
         
        $.ajax({
        type: "POST",
        url: "<?php echo site_url(); ?>mail/send",
        data: dataString,
         
        success: function(data){
            updateMessages();
            $(".mail_conversation_answer_input").val('');
        }
                  
        });
                 
        return false;
    });
});
  • 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-17T18:49:32+00:00Added an answer on June 17, 2026 at 6:49 pm

    You should provide functions instead of strings to setTimeout/setInterval functions. And also there’s no need for you to set interval on window load event. You can just keep it as part of DOM ready:

    $(function() {
        updateMessages(); // don't wait 2 seconds for first update
        setInterval(updateMessages, 2000); // update every 2 seconds
    });
    

    Everything else seems to should work as expected as long as your posback work when no data is being received (ref dataString).

    I hope you do realise that you’re using implied globals and understand why that may be a big problem (ref dataString again).

    How I would rewrite your code

    I would rewrite your whole code into the following that removes implied global variable dataString, doesn’t pollute global scope with additional functions and uses setTimeout instead of interval which may in some cases be problematic (although in your case since it’ only runs every 2 seconds it shouldn’t be a problem if there’s no additional very complex client-side script execution)

    I’ve kept everything within function closure local scope:

    $(function() {
    
        var timeout = null;
    
        var form = $("#mail_send").submit(function(evt){
            evt.preventDefault();
            $(".mail_conversation_answer_input", form).val("");
            updateMessages();
        });
    
        var updateMessages = function() {
    
            // we don'w want submit to interfere with auto-updates
            clearTimeout(timeout);
    
            $.ajax({
                type: "POST",
                url: "<?php echo site_url(); ?>mail/send",
                data: form.serialize(),
                success: function(data){
                    $("#mail_container_conversation").html(data);
                    timeout = setTimeout(updateMessages, 2000);
                }
            });
        };
    
        // start updating
        updateMessages();
    
    });
    

    This code requires your server side (processing on /mail/send) to understand that when nothing is being posted (no data) that it doesn’t add empty line in the conversation but rather knows that this is just an update call. This functionality now uses only one server-side URL and not two of them. If you’d still require two, then this code should do the trick:

    $(function() {
    
        var timeout = null;
        var url = {
            update: "<?php echo site_url();?>mail/ajaxupdate/<?php echo $user;?>",
            submit: "<?php echo site_url();?>mail/send",
            use: "update"
        };
    
        var form = $("#mail_send").submit(function(evt){
            evt.preventDefault();
            url.use = "submit";
            $(".mail_conversation_answer_input", form).val("");
            updateMessages();
        });
    
        var updateMessages = function() {
    
            // we don'w want submit to interfere with auto-updates
            clearTimeout(timeout);
    
            $.ajax({
                type: "POST",
                url: url[url.use],
                data: form.serialize(),
                success: function(data){
                    $("#mail_container_conversation").html(data);
                    url.use = "update";
                    timeout = setTimeout(updateMessages, 2000);
                }
            });
        };
    
        // start updating
        updateMessages();
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this

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.