我做了一個簡單的聊天應用程序,一切工作正常, ,但請讓我知道如何停止附加相同的數據,如循環在setInterval的ajax調用中, 。這是我的代碼如何停止從ajax呼叫追加相同的數據爲mysql造成setInterval
setInterval(function() {
get_ajax_msg();
}, 5000);
function get_ajax_msg(){
//AJAX CALL//
$.ajax({
url: "msg.php",
data:'act=check&id='+call_id+'&fid='+call_fid,
type: "POST",
success: function(data) {
if(data !=""){
$('#ctlist').append(data);
}
}
});
//END AJAX CALL//
}
PHP代碼
$msg_id= $_POST['id'];
$runmsg = $db->query("SELECT * FROM chat_msg WHERE pid=". $msg_id ." ORDER BY date DESC LIMIT 1");
while ($mymsg = $db->fetch_array($runmsg)) {
echo $mymsg['msg'];
}
你試過了什麼? –
獲取聊天框的消息 –
簡單:不要獲取所有消息。只有在您上次要求更新後纔會收到消息。這意味着你需要做'where date> $ last_request'類型的東西。 –