简体   繁体   中英

Long polling with jquery/ajax on server side

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.

I figured it out from this question: Long polling locking up other AJAX calls … - php locks a given session until the page is done loading so the second ajax call wasn't able to go through. You have to release the lock by calling session_write_close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM