Javascript 长轮询脚本停止所有其他脚本
我使用下面的脚本开始对php文件进行长时间轮询。。它检查是否更新了任何结果并发送响应 由于某种原因,当插入此javascript时,所有其他脚本都会挂起一个长时间的fire bug轮询Javascript 长轮询脚本停止所有其他脚本,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我使用下面的脚本开始对php文件进行长时间轮询。。它检查是否更新了任何结果并发送响应 由于某种原因,当插入此javascript时,所有其他脚本都会挂起一个长时间的fire bug轮询 function waitForMsg(){ $.ajax({ type: "GET", url: "auth/classes/getdata.php", async: true, cache: false, success: function(data){ c
function waitForMsg(){
$.ajax({
type: "GET",
url: "auth/classes/getdata.php",
async: true,
cache: false,
success: function(data){
console.log(data)
setTimeout("waitForMsg()",1000);
},
error: function(XMLHttpRequest,textStatus,errorThrown) {
// alert("error: "+textStatus + " "+ errorThrown );
setTimeout("waitForMsg()",15000);
}
});
}
$(document).ready(
function()
{
waitForMsg();
});
这是php文件getdata.php
require_once($_SERVER['DOCUMENT_ROOT'].'/auth/config/db.php');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$user_id = $_SESSION['user_id'];
$lastmodif = time();
$update = 1;
while ($update <= $lastmodif) {
usleep(10000);
clearstatcache();
$sql = "select ua.user_id as member,ua.post_id,pa.user_id,pa.type,pa.time,CONCAT(u.first_name,' ',u.last_name) as
name,u.thumbnail from user_activity ua right join post_activity pa on
ua.post_id=pa.post_id right join users u on pa.user_id=u.user_id where
ua.user_id=".$user_id." and pa.time > FROM_UNIXTIME('".$lastmodif."')";
$result = $conn->query($sql) or die(mysqli_error());
if ($conn->affected_rows > 0) {
$update=$lastmodif;
$response = array();
$response['msg'] ='update';
echo json_encode($response);
}
}
require_once($_SERVER['DOCUMENT_ROOT'.]./auth/config/db.php');
$conn=newmysqli(DB_主机、DB_用户、DB_通行证、DB_名称);
$user\u id=$\u会话['user\u id'];
$lastmodif=time();
$update=1;
while($updatequery($sql)或die(mysqli_error());
如果($conn->受影响的行>0){
$update=$lastmodif;
$response=array();
$response['msg']='update';
echo json_编码($response);
}
}
很确定你的问题是
usleep(10000);
这将有效地停止执行和您尝试使用
setTimeout(“waitForMsg()”,1000)启动的ajax循环执行逻辑的usleep块。你在Firebug的控制台面板/选项卡中是否收到任何错误消息或警告?不,这是奇怪的事情我认为这叫做ajax轮询,不是长轮询->它不会每秒左右发送一个ajax请求。会话初始化后它会挂起大约40分钟?你似乎依赖于$\u SESSION['user\u id'];
-这是从哪里来的?删除了usleep..但仍然不起作用..我插入了脚本,但没有在html中执行..一切加载正常..运行waitForMsg()的那一刻;着火错误..所有其他Ajax停止工作并挂起
<?
if(!define("_IS_GET"))
exit("Use another form to get get awww");
else
{
ignore_user_abort(TRUE);
set_time_limit(0); // adjust this to trap long time load
//...your lame process here
//sample you wanna dogetdata.php
$prId = shell_exec("nohup -f php '/path/to/your/getdata.php' /dev/null 2&<1 & $!",$display);
while(exec("$prId -s"))
echo $display."\n<BR>";
// note killing your jobs can be done using prId so its your initiative to store it and keep it ?
}
?>