<script type="text/javascript">
var maxID = 0;
function showmsg()
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState==4){
eval("var jn_info="+xhr.responseText);
var s = "";
for(var i=0; i < jn_info.length; i++){
s += '<p>'+jn_info[i].jieshuo+" :"+jn_info[i].content+"</p>";
maxID = info[i].id;
}
var hm =document.getElementById('show_msg');
hm.innerHTML += s;
hm.scrollTop = hm.scrollHeight;
}
}
xhr.open('get','./jieshuo_data.php?maxID='+maxID);
xhr.send(null);
}
window.onload = function(){
setInterval("showmsg()",2000);
}
</script>
//处理页面
$maxID = $_GET['maxID'];
$sql = "select * from jieshuo where id>".$maxID;
$rs=mysqli_query($conn,$sql);
$info = array();
while($row= mysqli_fetch_assoc($rs)){
$info[] = $row;
}
echo json_encode($info);
{
兄弟,你还是把代码格式化下,然后我在帮你看看,你这个看的头疼
<script type="text/javascript">
var maxID = 0;
function showmsg()
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState==4){
eval("var jn_info="+xhr.responseText);
var s = "";
for(var i=0; i < jn_info.length; i++){
s += '<p>'+jn_info[i].jieshuo+" :"+jn_info[i].content+"</p>";
maxID = info[i].id; //没看懂这句有何意义,你也没有比较最大id啊,还有maxID = info[i].id; 这个你是不是调用错了?哪来的info,不是jn_info吗?
}
var hm =document.getElementById('show_msg');
hm.innerHTML += s;
hm.scrollTop = hm.scrollHeight;
}
}
xhr.open('get','./jieshuo_data.php?maxID='+maxID);
xhr.send(null);
}
window.onload = function(){
setInterval("showmsg()",2000);
}
</script>
为什么不用jQuery里的ajax呢
<script type="text/javascript">
var maxID = 0;
function showmsg()
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState==4){
eval("var jn_info="+xhr.responseText);
var s = "";
for(var i=0; i < jn_info.length; i++){
s += '<p>'+jn_info[i].jieshuo+" :"+jn_info[i].content+"</p>";
maxID = info[i].id;
}
var hm =document.getElementById('show_msg');
hm.innerHTML += s;
hm.scrollTop = hm.scrollHeight;
}
}
xhr.open('get','./jieshuo_data.php?maxID='+maxID);
xhr.send(null);
}
window.onload = function(){
setInterval("showmsg()",2000);
}
</script>
///=换成下面的=============================================
<script type="text/javascript">
var maxID = 0;
function showmsg(){
$.ajax({
url: "./jieshuo_data.php",
type: 'get',
data: {
maxID:maxID
},
dataType: 'JSON',
timeOut: 3000,
success: function (data) {
console.log(data)
// for(var i=0; i < data.length; i++){
// s += '<p>'+data[i].jieshuo+" :"+data[i].content+"</p>";
// maxID = data[i].id;
// }
// var hm =document.getElementById('show_msg');
// hm.innerHTML += s;
// hm.scrollTop = hm.scrollHeight;
}
})
}
</script>