我正在处理通知消息 我想使用 Ajax 和 Jquery 每 10 秒从页面调用 check_new-reply.php
加载新消息,但我的代码没有显示任何我不知道的内容'知道错误是什么,有人可以帮我吗?
<script>
$(document).ready(function(){
$(function(){
var timer = 10;
var test = "";
function inTime(){
setTimeOut(inTime, 1000);
$("#timer-u").html("Time refreshing"+timer);
if(timer == 8){
$("#message-u").html("Loading....");
$.POST("check_new_reply.php",{testing:test}, function(data){
$("#message-u").html(data);
})
timer = 11;
clearTimeout(inTime);
}
timer--;
}
inTime();
});
});
</script>
这是PHP
<?php include($root . '_inc/Initialization.php');?>
<?php require_once("_inc/dbcontroller.php"); $db_handle = new DBController();?>
<?php
$users = $_SESSION['username'];
$newquery = "SELECT * FROM blog_post
INNER JOIN replys
ON blog_post.UserName = '$users'
WHERE replys.read = 0
ORDER BY rtime";
$newhisory = mysql_query($newquery);
while($newrow = mysql_fetch_array($newhisory)){
echo '<div class="fnot"><a href="/questions/postid/'.$newrow['BID'].'" id="readme">'.htmlentities($newrow['blog_title']).'</a>';
echo '<span class="ttcredit"><font color="darkgreen">94</font> </span> <a class="reqttag reqttag2" href="#">No</a> ';
echo '</div>';
echo '<input type="hidden" id="unr" name="unr" value="'.$newrow['BID'].'"/>';
}
?>
最佳答案
如果您只想每 10 秒调用一次,请在 setTimeOut
中使用 10000
毫秒。此外,最好仅在上一次 Ajax 调用完成后再次调用该函数:
$(document).ready(function(){
$(function(){
var test = "";
function inTime(){
$.POST("check_new_reply.php",{testing:test}, function(data){
$("#message-u").html(data);
setTimeout(inTime, 10000);
});
}
inTime();
});
});
关于php - Ajax 与 jquery 设置时间间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35305597/