最新消息: 生命不止,奋斗不息……

PHP实现在线聊天

PHP admin 1153浏览 0评论

2539467239900637843

//发送消息
function sendMsg()
{
    var msg = $(“#msg”).val();
    $(“#msg”).val(“”);
    $.post(“msg.php”, {“type”:”send”,”id”:$(“#id”).val(),”tid”:$(“#tid”).val(),”msg”:msg},function(msgid){
    $(“#msglist ul”).append(‘</pre>
      <ul>
	<li>me:’+msg+’</li>
      </ul>
    <pre>

  ’);
   var scrollTop = $(“#msglist ul”).height();
   $(“#msglist”).scrollTop(scrollTop);
   $(“#msgid”).val(msgid);
   if(count>5)
   {
      clearTimeout(interval);  //关闭慢的定时器
      interval = setInterval(getMsg, speedTime);
      count = 0;
   }
  });
}
//获取消息
function getMsg()
{
    var msgid = $(“#msgid”).val();
    $.post(“msg.php”,  {“type”:”get”,”id”:$(“#id”).val(),”tid”:$(“#tid”).val(),”msgid”:msgid},function(msg){
   if(msg!=”empty”)
   {
      var data = eval(“(“+msg+”)”);
      for(var i=0;i<data.length;i++)
      {
        $(“#msgid”).val(parseInt(data[i].id)+1);
        $(“#msglist ul”).append(‘</pre>
         <ul>
	   <li>re:’+data[i].msg+’</li>
         </ul>
    <pre>
  ’);
}
var scrollTop = $(“#msglist ul”).height();
$(“#msglist”).scrollTop(scrollTop);
if(count>maxCount)
{
    clearTimeout(interval);  //关闭慢的定时器
    interval = setInterval(getMsg, speedTime);
    count = 0;
}
}else
{
   count ++;
if(count>maxCount)
{
   clearTimeout(interval);  //关闭快速定时器
   interval = setInterval(getMsg, slowTime); //创建一个新的定时器
}
}
});
}
//设置时间
var speedTime = 1000;
var slowTime = 20000;
//设置次数
var count = 0;
var maxCount = 10;
//创建一个定时器
var interval;
$(function(){
  $(“#send”).click(function(){
  sendMsg();
  });
  $(“#clear”).click(function(){
  $(“#msglist ul”).html(“”);
});

  interval = setInterval(getMsg, speedTime);
  //clearTimeout(interval);  //关闭定时器
});

转载请注明:IT世界 » PHP实现在线聊天

您必须 登录 才能发表评论!