I am doing long polling with multiple pages in a single page. The problem is that when I have a new request, it first completes the previous request although i abort the previous request when there is a new request.
this is my jquery code:
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;
}
}
My 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
}
}
When I click on home image after clicking on school image it takes a while (completes the first request) before it proceeds with the home image request even though I used request.abort(). If I don't use request.abort() it takes the same amount of time and gives me a timed out error and then processes the home image request. How do I make it ignore the previous ajax request quickly so that it will quickly go to the new request and process it.