php - Ajax 与 jquery 设置时间间隔

标签 php jquery ajax

我正在处理通知消息 我想使用 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/

相关文章:

php - 使用 ajax、jquery 和 PHP 更新数据库中的图像不起作用

php - Symfony 从模型生成数据库

javascript - 我的 JavaScript 函数/if 语句仅在按特定顺序放置时才有效

jquery - 链接停止工作——jquery 问题?

javascript - 即使元素的值也在 CSS 中设置,也获取内联样式值?

javascript - 如何在单击超链接时触发第二个 AJAX 调用?

ajax - 用于调用 ajax 的按钮的 Symfony Crawler

javascript - 如何使用ajax将数据库值发布到下一页

php - Exception([string $exception [, long $code ]]) 参数错误

jQuery 正则表达式验证 : unsigned integer or float (max 2 decimal), 只有点,没有逗号