这段代码有错误吗? 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/