我刚开始做彗星,做一个简单的应用程序。我的html文件看起来像
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>testing comet</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-1.9.0.js"></script>
</head>
<body>
<div id="content">
</div>
<p>
<form action="" method="get" onsubmit="comet.doRequest($('#word').val());$('#word').val('');return false;">
<input type="text" name="word" id="word" value="" />
<input type="submit" name="submit" value="Send" />
</form>
</p>
<script type="text/javascript">
// comet implementation
var Comet = function (data_url) {
this.timestamp = 0;
this.url = data_url;
this.noerror = true;
//alert(data_url);
this.connect = function() {
var self = this;
$.ajax({
type : 'get',
url : this.url,
dataType : 'json',
data : {'timestamp' : self.timestamp},
success : function(response) {
self.timestamp = response.timestamp;
self.handleResponse(response);
self.noerror = true;
},
complete : function(response) {
// send a new ajax request when this request is finished
if (!self.noerror) {
alert("error");
// if a connection problem occurs, try to reconnect each 5 seconds
setTimeout(function(){ comet.connect(); }, 5000);
}else {
// persistent connection
self.connect();
}
self.noerror = false;
}
});
}
this.disconnect = function() {}
this.handleResponse = function(response) {
$('#content').append('<div>' + response.msg + '</div>');
}
this.doRequest = function(request) {
$.ajax({
type : 'get',
url : this.url,
data : {'msg' : request}
});
}
}
var comet = new Comet('./backend.php');
comet.connect();
</script>
</body>
</html>backend.php看起来就像
<?php
$dr=DIRECTORY_SEPARATOR;
$filename = dirname(__FILE__).$dr.'data.txt';
// store new message in the file
$msg = isset($_GET['msg']) ? $_GET['msg'] : '';
if ($msg != '')
{
file_put_contents($filename,$msg);
die();
}
// infinite loop until the data file is not modified
$lastmodif = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$currentmodif = filemtime($filename);
while ($currentmodif <= $lastmodif) // check if the data file has been modified
{
usleep(10000); // sleep 10ms to unload the CPU
clearstatcache();
$currentmodif = filemtime($filename);
}
// return a json array
$response = array();
$response['msg'] = file_get_contents($filename);
$response['timestamp'] = $currentmodif;
echo json_encode($response);
flush();data.txt包含一个空白文件。
现在我的问题是
if (!self.noerror) {
alert("error");这将始终执行并显示警报“错误”。但是如果我评论一下警报部分的话,它就能很好的工作了。出什么事了吗?
当我使用firebug跟踪进程时,在这些请求上,会得到这样一个致命的错误。

任何一个人请提前帮我谢谢
发布于 2013-09-25 04:06:35
超时问题将由been循环在PHP退出之前未能退出造成,因为已经达到了max_execution_time指令的值。您可以扩展它,但是您真的希望脚本运行那么长时间吗?我更倾向于在JavaScript中做更多的工作--让它每秒钟请求一次更新(10 is太频繁了)。
对于alert("error"),在设置noerror值时要小心--您真的想在complete()函数的末尾将其设置为false吗?我认为您希望将您对$.ajax()的调用更改为类似于以下(缩写)的内容:
$.ajax({
error : function() {
// the request failed
setTimeout(function() { comet.connect(); }, 5000);
},
success : function(response) {
// the request succeeded
self.connect();
}
});如果可以的话,完全删除noerror变量,并使用error()和success()回调来容纳每个场景的逻辑,如上面所示。
发布于 2013-09-25 03:51:10
您必须编辑backend.php行;
<?php
$dr=DIRECTORY_SEPARATOR;
ini_set('max_execution_time', 300);
$filename = dirname(__FILE__).$dr.'data.txt';https://stackoverflow.com/questions/18995828
复制相似问题