代码在启动时工作得很好,但是逐渐地(在2,3秒之后)它会减慢并减慢服务器速度.Plz帮助我识别错误或建议任何其他方法。
我现在对此感到厌烦。
var last_msg_id = 2;
function load_msgs() {
$.ajax({
type:"Post",
url:"getdata.php",
data:{
last_msg_id:last_msg_id
},
dataType:"json",
async:true,
cache:false,
success:function(data) {
var json = data;
$("#commidwin").append(json['msg']);
last_msg_id = json["last_msg_id_db"];
setTimeout("load_msgs()", 1000);
},
error:function(XMLhttprequest, textstatus, errorthrown) {
alert("error:" + textstatus + "(" + errorthrown + ")");
setTimeout("load_msgs()", 15000);
}
});
}
Php文件在这里。
$last_msg_id=$_POST['last_msg_id'];
$last_msg_id_db=1;
while($last_msg_id>$last_msg_id_db) {
usleep(10000);
clearstatcache();
$sql = mysqli_query($db3->connection,"SELECT * FROM chat_com where id>'$last_msg_id' ORDER by id ASC");
$sql_m = mysqli_query($db3->connection,"SELECT max(id) as maxid FROM chat_com");
$row_m = mysqli_fetch_array($sql_m);
$last_msg_id_db = $row_m['maxid'];
while($row = mysqli_fetch_array($sql)){
$textt = $row['mesg'];
$last_msg_id_db = $last_msg_id_db;
$response = array();
$response['msg'] = $textt;
$response['last_msg_id_db'] = $last_msg_id_db;
}
}
echo json_encode($response);