我试图进行实时聊天,但我遇到了一个问题:setInterval(用于检查新消息)不起作用:/...
这是我的 jQuery 代码
$(function(){
setInterval(function() {
$.ajax({
url: "./ajax/checknew.php",
success: function(data) {
$("#chMsgCont").append(data);
}
});
},1000);
$('#chMsgCont').load('./ajax/msg.php', function() {
var div = $('#chMsgCont');
var o = div.offset().top;
div.scrollTop( o + 12302012 );
});
});
我的 msg.php
<?php
include("../system/config.site.php");
$query = mysql_query("SELECT * FROM chat_msg ORDER BY timestamp ASC");
while($p = mysql_fetch_assoc($query)) {
$auth = mysql_fetch_assoc(mysql_query("SELECT * FROM chat_users WHERE id = '".$p['auth_id']."'"));
$update = mysql_query("UPDATE chat_msg SET new = 'no' WHERE id = '".$p['id']."'");
?>
<div class="chatMsg">
<p id="chatPMsg">
<span class="chatTime"><?php echo date("H:i", $p['timestamp']); ?></span>
<b><?php echo $auth['name']." ".$auth['surname']; ?></b><br />
<?php echo stripslashes($p['msg']); ?>
</p>
<p id="chatImg">
<img src="./images/thumb<?php echo $p['auth_id']; ?>.png" />
</p>
<div style="clear:both;"><!– –> </div>
</div>
<?php
}
?>
我的 checknew.php
<?php
include("../system/config.site.php");
$query = mysql_query("SELECT * FROM chat_msg WHERE new = 'yes' ORDER BY timestamp ASC");
if(mysql_num_rows($query) > 0) {
while($p = mysql_fetch_assoc($query)) {
$auth = mysql_fetch_assoc(mysql_query("SELECT * FROM chat_users WHERE id = '".$p['auth_id']."'"));
?>
<div class="chatMsg">
<p id="chatPMsg">
<span class="chatTime"><?php echo date("H:i", $p['timestamp']); ?></span>
<b><?php echo $auth['name']." ".$auth['surname']; ?></b><br />
<?php echo stripslashes($p['msg']); ?>
</p>
<p id="chatImg">
<img src="./images/thumb<?php echo $p['auth_id']; ?>.png" />
</p>
<div style="clear:both;"><!– –> </div>
</div>
<?php
}
} else {
echo "";
}
?>
setInterval 拒绝附加查询结果:/,这怎么可能?