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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:33:13+00:00 2026-05-18T07:33:13+00:00

Ok Hi Help help help I am having a major problem with setTimeout it

  • 0

Ok Hi
Help help help
I am having a major problem with setTimeout it is not working and i did everything not working

I am developping a chat system so i need to send and recieve messages (i test it by opening 2 browser windows)

Here is the code i changed many times
I hope any one can help me

javascript

$(document).ready(function()
{
    updateMsg();
});

function updateMsg()
{
$.ajax({
    url:"db.php",
    type:"POST",
    success:function(data){
    }
});
setTimeout("updateMsg()",7000);
}

function addMessages()
{
$.ajax({
    url:"db.php",
    type:"POST",
    data:"name="+$("#name").val()+"&to="+$("#user2").val()+"& cc="+$("#user").val()+"&msg="+$("#msg").val(),  
    success:function(data)
    {
    $("#t1").prepend(data);}
});

}

php

$user_select = mysql_query("SELECT * FROM User WHERE User_name = '$name'") 
                        or die(mysql_error());
$fetch_select = mysql_fetch_array($user_select);
$user_check = mysql_num_rows($user_select);

if(isset($_POST['msg']) && $_POST['msg'] != '')
{
  if ($user_check == 0)
 {
mysql_query("INSERT INTO User (User_name) VALUES ('$name')") or die(mysql_error());

$new_user = mysql_query("SELECT * FROM User WHERE User_ID = LAST_INSERT_ID()");

$fetch_new_user = mysql_fetch_array($new_user);

mysql_query("INSERT INTO Messages(From_user,Msg_body,Date_Time) 
             VALUES ('$fetch_new_user[User_ID]','$msg',NOW())") or     die(mysql_error());
  }

  else 
    {
        mysql_query("INSERT INTO Messages(From_user,Msg_body,Date_Time) 
             VALUES ('$fetch_select[User_ID]','$msg',NOW())") or   die(mysql_error());
   }
   }

  $sql = mysql_query("SELECT Msg_body,Date_Time,User_name
                FROM Messages,User
                WHERE From_user = User_ID
                AND Msg_ID = LAST_INSERT_ID()
                ORDER BY Date_Time DESC") or die(mysql_error());

while($result = mysql_fetch_array($sql))
   {
       $mydata = '<tbody id="tbody1">
 <tr class="highlight">
<td  width="30" id="bullet" align="center">
<a href="#" class="nohighlight">&#8226;</a></td>
<td width="30px" align="center" id="replyImg"><input type="image" src="css/images/reply_arrow.png" onClick="reply()"></input></td>
<td width="70" align="Left" id="time">'.$result["Date_Time"].'</td>
<td width="200" align="Left" id="from">'.$result["User_name"].'</td>
<td width="200" align="Left" id="to">'.$result[""].'</td>
    <td id="showMsg">'.$result["Msg_body"].'</td>
    <td width="200" align="left" id="group">'.$result["Grp_abr"].'</td>     
  </tr>
  </tbody>';

}

    echo $mydata;

 ?>

Thanks guys

  • 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-05-18T07:33:14+00:00Added an answer on May 18, 2026 at 7:33 am

    It is different than i first started but hey that is programming
    javascript
    a function to update messages on screen.

      var last_msg_id = 0;
      function updateMessage()
      {
    $.ajax({ 
        type: "post", 
        url: "db.php",
        data : {MsgNo :last_msg_id},
        dataType: "xml" ,
        success: function(data){
          $(data).find("message").each(function() {
                var msg_id = $(this).find("msg_id").text();
                var date_time = $(this).find("date_time").text();
                var from_user = $(this).find("from_user").text();
                var from_user_id = $(this).find("from_user_id").text();
                var from_group_id = $(this).find("from_group_id").text();    
                var from_group = $(this).find("from_group").text();
                var from_grp_abr = $(this).find("from_grp_abr").text();
                var from_kind = $(this).find("from_kind").text();
                var row_bg_clr = $(this).find("from_bg_color").text();
                var to_user = $(this).find("to_user").text();
                var to_user_id = $(this).find("to_user_id").text();
                var to_group = $(this).find("to_group").text();
                var to_group_id = $(this).find("to_group_id").text();
                var to_grp_abr = $(this).find("to_grp_abr").text();
                var to_bg_color = $(this).find("to_bg_color").text();
                var to_kind = $(this).find("to_kind").text();
                var msg_type = $(this).find("msg_type").text();
                var msg = $(this).find("msg").text();
                var onlydate = getonlydate(date_time);
                var class_flag = ($(this).find("flagged").text() == 'true') ?    'flagged' : 'unflag';
    
                var reply = (to_group_id == $("#group_id").val()) ? 'reply' : '';
    
                var handle_reply = (from_user == "") ? from_grp_abr : from_user;
                //var styleclass = (8)? 'smoke' : 'blue';
    
               if (from_kind == 3)
               {
                 var centerDiv = 'centerPopup(event,"changeObsLabel");';
                 var change_label = 'change_label('+msg_id+',"'+msg_type+'","changeObsLabel");';
               }
               else if (from_kind == 4)
               {
                 var centerDiv = 'centerPopup(event,"changeUserLabel");';
                 var change_label = 'change_label('+msg_id+',"'+msg_type+'","changeUserLabel");' ;
               }
               else 
               {
                 var centerDiv = 'centerPopup(event,"changeAnimLabel");';
                 var change_label = 'change_label('+msg_id+',"'+msg_type+'","changeAnimLabel");' ;
               }
    
                var html = "<tr class='active' style='background-color:#"+row_bg_clr+"' id='row_"+msg_id+"'>";
                html += "<td><a class='"+reply+"' onclick=\"reply('"+escape(handle_reply)+"','"+escape(to_user)+"',"+from_user_id+","+to_user_id+");\">  <\/a><\/td>";
                html += "<td class='time'>"+date_time+"<\/td>";
                html += "<td>"+from_user+"&nbsp;["+from_grp_abr+"]"+"<\/td>";
                html += "<td>"+to_user+"&nbsp;["+to_grp_abr+"]"+"<\/td>";
                html += "<td><a id='type_of_msg_"+msg_id+"' class="+msg_type+" onclick="+centerDiv+change_label+"> <\/a><\/td>";
                html += "<td><a id='flag_"+msg_id+"' class='"+class_flag+"' onclick=\"flagMsg("+msg_id+",'"+row_bg_clr+"')\">  <\/a><\/td>";
                html += "<td>"+msg+"&nbsp;"+"<span style='color:gray'>["+msg_id+"]</span><\/td>";
                html += "<td>"+from_grp_abr+"<\/td><\/tr>";
    
                $("#tbody1").prepend(html);
                  //append data to tab2 messages received
                 if (to_group == $("#login").val())
                 {
                   $("#tbody2").prepend(html);
                 }
    
                 //append data to tab3 sent messages
                 else if (from_group == $("#login").val())
                 {
                   $("#tbody3").prepend(html);
                 }
    
                 if (class_flag == 'flagged')
                 {
                   $("#tbody4").prepend(html);
                 }
    
                 if(to_kind == 3 || from_kind == 3){
                    $("#tbody5").prepend(html);
                 }
    
                 if(from_group != $("#login").val())
                 {
                   $("#tbody"+from_group).prepend(html);
                 }
    
                 if(to_group != $("#login").val())
                 {
                   $("#tbody"+to_group).prepend(html);
                 }
    
                 last_msg_id = msg_id;
            });
            alternateRows();
            setTimeout('updateMessage()', 3000);
      },
        error: function(){
            setTimeout('updateMessage()', 3000);
        },
        async: false
    });
     }
    

    a function to insert in database new messages

    function addMessages()
    {
     if(document.getElementById('name').value == "")
    {
        alert("Missing a sender");
    }
    else if (document.getElementById('user2').value == "")
    {
        alert("Missing a receiver");
    }
    else if (document.getElementById('msg').value == "")
    {
        alert("Missing a message");
    }
    else {
    $.ajax({
        url:'insert.php',
        type:'post',
        data:{name: $("#name").val(), from_user_id:$("#from_user_id").val(),from_grp_id:$("#from_grp_id").val(),user2: $("#user2").val(), to_user_id: $("#to_user_id").val(),to_grp_id:$("#to_grp_id").val(), message:$("#msg").val(), label:$("#label").val()},
        success:function(data)
        {
          if (data == 'new user')
          {
            checkNewRole();
            $("#name").val("");
            $("#user2").val("");
            $("#msg").val("");
            $("#label").val("empty");
          }
          else 
          {
           $("#name").val("");
           $("#user2").val("");
           $("#msg").val("");
           $("#label").val("empty");
          }
        }
    });
      }
     }
    

    As for php it remained more or less the same.

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

Sidebar

Related Questions

I am having trouble understanding how the System Registry can help me convert a
Help me ..my page index is not working in visual studio. my page load
We're having a small issue and could use some help - we have the
In general, what kinds of design decisions help an application scale well? (Note: Having
Help! I have an Axis web service that is being consumed by a C#
Help! I am using jQuery to make an AJAX call to fill in a
Help! I have a PHP (PHP 5.2.5) script on HOST1 trying to connect to
help me to manage IIS (what i am trying to do is create virtual
Help I am busy making changes to a type library in a Datasnap project.
HELP! I just setup a virtual host for two sites that have a lot

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.