views:

467

answers:

2

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  A: 

It's very unlikely that there's anything wrong with setTimeout. You can examine this yourself by having your updateMsg() do something visible when it runs, other than start that Ajax request; do an alert() or change the color of a box on the page, or something.

The most obvious problem you have, if this is the code you're running, is that your callback function from the Ajax call is empty, so isn't doing anything.

chaos
+1  A: 

It probably isn't reaching setTimeout(). Put an alert() in there.

Also, you should have setTimeout() inside the callback function. Otherwise if the HTTP requests take too long, you'll start doing multiple HTTP requests will the old ones are still pending. This wills surely crash the server if it is already overloaded or build up the HTTP request queue in the browser if you go over the limit (usually its two separate requests at a time)

Putting setTimeout() inside the callback makes sure the calls are synchronous/linear. So you only have one HTTP request at a time.

ie:

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

Should be:

function updateMsg()
{
$.ajax({
        url:"db.php",
        type:"POST",
        success:function(data){
          // after processing your stuff


          // make sure the next request is linear
          setTimeout("updateMsg()",7000);
        },
        fail: function() { /* handle your failed reqeust */ }
});

}

That doesn't solve your problem, but it will solve problems you'll have later on. You'll also need to handle failed requests.

bucabay