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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:52:07+00:00 2026-06-12T16:52:07+00:00

I wrote a little chat plugin that i’ll need to use on my site.

  • 0

I wrote a little chat plugin that i’ll need to use on my site. It works with a simple structure in HTML, like this:

<div id="div_chat">
  <ul id="ul_chat">
  </ul>
</div>

<div id="div_inputchatline">
  <input type="text" id="input_chatline" name="input_chatline" value="">
  <span id="span_sendchatline">Send</span>
</div>

There’s a ‘click’ bound event on that Span element, of course. Then, when the user inserts a message and clicks on the “Send” span element, there’s a Javascript function with calls an Ajax event that inserts the message into the MySQL database:

function function_write_newchatline()
{
  var chatline = $('#input_chatline').val();

  $.ajax
  ({
    type: "POST",
    url: "ajax-chat-writenewline.php", //1: ok, 0: errore
    data: ({'chat_line': chatline}),
    dataType: "text",
    cache: false,
    success: function(ajax_result)
    {
      function_get_newchatlines();
    }
  });
}

And, in case the message is successfully inserted into DB, it calls a function to read new lines and put them in HTML structure i posted before:

function function_get_newchatlines()
{
  $.ajax
  ({
    type: "POST",
    url: "ajax-chat-loadnewlines.php", //1: ok, 0: errore
    data: '',
    dataType: "text",
    cache: false,
    success: function(ajax_result) //example of returned string: 'message1>+<message2>+<message3'
    {
      //explode new chat lines from returned string
      var chat_rows = ajax_result.split('>+<');
      for (id_row in chat_rows)
      {
        //insert row into html
        $('#ul_chat').prepend('<li>' + chat_rows[id_row] + '</li>');
      }
      $('#span_sendchatline').html('Send');
    }
  });
}

Note: ‘ajax_result’ only contains html entities, not special chars, so even if a message contains ‘>+<‘, it is encoded by the php script called with Ajax, before being processed from this JS function.

Now, comes the strange behaviour: when posting new messages Opera, Firefox and even IE8 works well, as intended, like this:

A screen taken from Firefox

But, when i open Chrome window, i see this:

A screen taken from Chrome

As you can see, in Chrome the messages are shown multiple times (increasing the number each time, up to 8 lines per message). I checked the internal debug viewer and it doesn’t seem that the “read new lines” function is called more than one time, so it should be something related to Jquery events, or something else.

Hope i’ve been clear in my explanation, should you need anything else, let me know 🙂

Thanks, Erenor.

EDIT

As pointed out by Shusl, i forgot to mention that the function function_get_newchatlines() is called, periodically, by a setInterval(function_get_newchatlines, 2000) into Javascript.

EDIT2

Here’s is a strip of the code from the PHP file called by Ajax to get new chat lines (i don’t think things like “session_start()” or mysql connection stuff are needed here)

//check if there's a value for "last_line", otherwise put current time (usually the first time a user logs into chat)
if (!isset($_SESSION['prove_chat']['time_last_line']) || !is_numeric($_SESSION['prove_chat']['time_last_line']) || ($_SESSION['prove_chat']['time_last_line'] <= 0))
{
  $_SESSION['prove_chat']['time_last_line'] = microtime(true);
}

//get new chat lines
$result = mysql_query("select * from chat_module_lines where line_senttime > {$_SESSION['prove_chat']['time_last_line']} order by line_senttime asc; ", $conn['user']);
if(!$result || (mysql_num_rows($result) <= 0))
{
  mysql_close($conn['user']); die('2-No new lines');
}
//php stuff to create the string
//....
die($string_with_chat_lines_to_be_used_into_Javascript);

Anyway, i think that, if the problem was this PHP script, i would get similar errors in other browsers, too 🙂

EDIT4

Here’s the code that binds the click event to the “Send” span element:

$('#span_sendchatline').on('click', function()
{
  //check if there's already a message being sent
  if ($('#span_sendchatline').html() == 'Send')
  {
    //change html content of the span element (will be changed back to "send"
    //when the Ajax request completes)
    $('#span_sendchatline').html('Wait..');
    //write new line
    function_write_newchatline();
  }
  //else do nothing
});

(Thanks to f_puras for adding the missing tag 🙂

  • 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-12T16:52:09+00:00Added an answer on June 12, 2026 at 4:52 pm

    I would do one of the following:

    option 1:

    stop the timer just before the ajax call in function_write_newchatline() and start the timer when the ajax call returns.

    function function_write_newchatline()
    {
      var chatline = $('#input_chatline').val();
    
      stop_the_timer();
      $.ajax
      ({
        type: "POST",
        url: "ajax-chat-writenewline.php", //1: ok, 0: errore
        data: ({'chat_line': chatline}),
        dataType: "text",
        cache: false,
        success: function(ajax_result)
        {
          function_get_newchatlines();
        },
        complete: function() {
          start_the_timer();
        }
      });
    }
    

    option 2:

    Not call function_get_newchatlines() at all in the success event of the ajax call. Let only the timer retrieve the chat entries.

    function function_write_newchatline()
    {
      var chatline = $('#input_chatline').val();
    
      $.ajax
      ({
        type: "POST",
        url: "ajax-chat-writenewline.php", //1: ok, 0: errore
        data: ({'chat_line': chatline}),
        dataType: "text",
        cache: false,
        success: function(ajax_result)
        {
          // do nothing
        }
      });
    }
    

    I think there is some race condition between the function_get_newchatlines() that is called after a chat entry is added by the user and the periodical call of function_get_newchatlines() by the timer.

    option 3:

    Use setTimeout instead of setInterval. setInterval can mess things up when the browser is busy. So in the end of the setTimeout function call setTimeout again.

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

Sidebar

Related Questions

I wrote a little program that use activemq embedded broker. Program run on one
I wrote a little program, which is working like a ping command(i use ICMPSendEcho2),
I wrote this little web app that lists the websites running on the local
I wrote this little BASH script that creates a folder,unzips Wordpress and creates a
I wrote this little snippet that should format money but its failing on the
I wrote a little daemon in Perl that calls up FFMpeg to encode a
I wrote a little search script for a client, it works and words get
I wrote a little program that solves 49151 sudoku's within an hour for an
I wrote a little utility class that saves BitmapSource objects to image files. The
I wrote a little Java servlet that would dynamically generate an image button given

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.