长轮询脚本会停止所有其他脚本
long polling script stops all other scripts
我使用以下脚本启动php文件的长轮询。。它检查是否有任何结果被更新并发送响应。。
由于某种原因,当这个javascript被插入时,所有其他脚本都挂在一个长轮询上
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);
}
}
很确定您的问题是
usleep(10000);
这将有效地停止执行和您试图用执行逻辑的setTimeout("waitForMsg()",1000);
-usleep块启动的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 ?
}
?>
相关文章:
- java脚本在Chrome和其他浏览器中对LocaleTimeString的不同行为
- 网页上失败的javascript会导致所有其他脚本失败
- 在BoilerplateJS浏览器加载所有脚本(在其他模块中),而不考虑激活的模块
- 如果其他是咖啡脚本
- 我的时间倒计时脚本在谷歌浏览器中工作正常,但在其他浏览器中打开时它显示为南楠
- Selenium异步脚本在自己的线程中阻塞其他脚本
- 编写脚本以关闭其他选项卡或浏览器
- Firefox扩展开发:如何在内容脚本中创建一个全局函数,以便其他加载的脚本文件可以访问它
- 如何测试jQuery和某些其他脚本是否已加载
- 不要为机器人(谷歌和其他)加载JS脚本以获得更好的性能
- 如何在访问其他url时在nodewebkit中注入脚本
- JS脚本由1个元素调用,而不是由其他3个元素调用
- 长轮询脚本会停止所有其他脚本
- 我用脚本创建了一个按钮.我可以通过点击按钮调用其他函数吗?我这样做,但失败了
- 在ASP中调用HTML以调用其他脚本
- 如果其他脚本'装载顺序未知
- 有没有一个简单的脚本可以向美国/加拿大游客显示脚本1,向其他游客显示脚本2
- Pjax.页面上的其他JS脚本不会;不起作用
- jquery破坏了其他脚本
- Meteor脚本确保在其他一切之前执行(在客户端上)