php - 5 分钟长轮询示例的简单指南不起作用

标签 php jquery mysql ajax

这段代码有错误吗? The Example here

我尝试制作相同的示例,但效果不佳

如果有人可以通过发送教程或任何帮助来帮助我,我想从数据库发出实时通知

注意:我在带有 wamp 服务器的本地主机上工作

HTML

index.php

<!doctype html>
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script>
            var timestamp = null;
    function waitForMsg () {
        $.ajax({
            type: "GET",
            url: "getData.php?timestamp="+timestamp,
            async: true,
            cache : false,
            success : function (data){
                var json = eval('(' + data + ')');
                if (json['msg'] != "") {
                    alert(json['msg']);
                }

                timestamp = json['timestamp'];
                setTimeOut("waitForMsg()" , 1000);
            },
            erorr : function  (XMLHttpRequest,textStatus, erorrThrown) {
                alert("erorr :" + textStatus + "(" + erorrThrown + ")");
                setTimeOut("waitForMsg()", 15000);
            }

        });
    }

    $(document).ready(function(){
        waitForMsg();
    });
</script>
</head>
<body>

</body>
</html>

PHP

getData.php

<?php 
$filename = dirname(__FLIE__)."/data.txt";
$lastmodif = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$currentmodif = filemtime($filename);

while($currentmodif <= $lastmodif){
    usleep(10000);
    clearstatcache();
    $currentmodif = filemtime($filename);
}

$response = array();
$response['msg'] = file_get_contents($filename);
$response['timestamp'] = currentmodif();
echo json_encode($response);

?>

最佳答案

$response['timestamp'] = currentmodif();

应该是

$response['timestamp'] = $currentmodif;

currentmodif 是变量而不是函数。

关于php - 5 分钟长轮询示例的简单指南不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17643475/

相关文章:

PHP 代码中的 JavaScript 内联验证

php - 从使用 AJAX 打开的页面访问父页面变量

php - 使用 php 在 Moodle 中创建类(class)

php - 在PHP中随机选择一个数组

javascript - 如何将对象添加到 jQuery 集合中指定索引处?

mysql - Visual Basic中 "?"和 "@"的区别

javascript - 在jquery中使用变量值作为数组名称

javascript - 如何使用 Javascript 将货币字符串转换为 double ?

mysql - 表中具有相同值的其他行的计数

php - 显示名称中含有特殊字符 "$"或 "-"的变量