Php 在服务器端使用jquery/ajax进行长轮询
我在一个页面中使用多个页面进行长时间轮询。问题是,当我有一个新请求时,它首先完成上一个请求,尽管当有一个新请求时我中止了上一个请求 这是我的jquery代码:Php 在服务器端使用jquery/ajax进行长轮询,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,我在一个页面中使用多个页面进行长时间轮询。问题是,当我有一个新请求时,它首先完成上一个请求,尽管当有一个新请求时我中止了上一个请求 这是我的jquery代码: var stopped = 1; var request = 0; $(document).ready(function () { $("#leftsec img").click(function () { stopped = 0; $("#leftsec img").each(funct
var stopped = 1;
var request = 0;
$(document).ready(function () {
$("#leftsec img").click(function () {
stopped = 0;
$("#leftsec img").each(function () {
$(this).attr({
src: 'images/' + this.id + '.gif'
});
$(this).hover(
function () {
$(this).attr("src", 'images/' + this.id + '_over.gif');
}, function () {
$(this).attr("src", 'images/' + this.id + '.gif');
});
});
$(this).attr({
src: 'images/' + this.id + '_over.gif'
});
$(this).hover(function() {
$(this).attr("src", 'images/' + this.id + '_over.gif');
}, function () {
$(this).attr("src", 'images/' + this.id + '_over.gif');
});
location.hash = '!' + this.id;
var topname = document.getElementById(this.id + '1').innerHTML;
$("#tophead").html(topname);
if(request != 0) {
request.abort();
}
var a = this.id;
$.ajax({
url: "no.php",
success: function (result) {
$.ajax({
url: "some.php?id=" + a,
success: function (result) {
$("#centerdata").html(result);
stopped = 1;
rec_fun(a);
}
});
}
});
});
if (window.location.hash != '') {
var hashvalue = window.location.hash;
hashvalue = hashvalue.split('!');
hashvalue = hashvalue[1];
$('#' + hashvalue).click();
} else {
$('#home').click();
}
});
function rec_fun(a) {
//alert('');
if (stopped == 1) {
request = $.ajax({
url: "some.php?id=" + a,
success: function (result) {
$("#centerdata").html(result);
rec_fun(a);
}
});
}
else {
return false;
}
}
我的HTML:
<div class="leftsec" id="leftsec">
<img id='home' class="" src="images/home.gif" onmouseover="this.src='images/home_over.gif'" onMouseOut="this.src='images/home.gif'" /><br />
<div id="home1" align="center" style="font-size:16px; font-family:Verdana;">Home</div>
<img id='school' class="" src="images/school.gif" onMouseOver="this.src='images/school_over.gif'" onMouseOut="this.src='images/school.gif'" /><br />
<div id="school1" align="center" style="font-size:16px; font-family:Verdana">My School</div>
</div>
家
我的学校
some.php:
if($_GET['id'] == 'home') {
$main = 'home';
for($i = 0; $i < 30; $i++) {
$word .= $main . "<br>";
}
}
if($_GET['id'] == 'school') {
$retschool = 0;
while($retschool != 1) {
// Look for an update
// If no update found sleep for 2 seconds
}
}
if($\u GET['id']=='home'){
$main='home';
对于($i=0;$i<30;$i++){
$word.=$main.“
”;
}
}
如果($_GET['id']=='school'){
$retschool=0;
而($retschool!=1){
//寻找更新
//如果未找到更新,则休眠2秒
}
}
当我在单击学校映像后单击主映像时,需要一段时间(完成第一个请求),然后才能继续执行主映像请求,即使我使用了request.abort()。如果我不使用request.abort(),它将花费相同的时间,并给我一个超时错误,然后处理主映像请求。我如何让它快速忽略以前的ajax请求,以便它快速转到新请求并处理它。我从这个问题中找到了答案:…-php锁定给定的会话,直到页面加载完毕,所以第二个ajax调用无法通过。您必须通过调用
session\u write\u close()来释放锁代码>您对request.abort()所做的操作应该是有效的-但是由于您的所有其他悬停/位置更改代码很难调试-尝试注释非ajax代码,然后看看它是如何运行的-看看您是否可以创建一个,没有任何区别,也找不到确切的问题。我认为这与有多个请求时的睡眠有关没有人能告诉我出了什么问题?