长轮询与服务器端的jQuery / AJAX [英] Long polling with jquery/ajax on server side

查看:100
本文介绍了长轮询与服务器端的jQuery / AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的长轮询在单页多页。问题是,当我有一个新的请求时,首先完成了previous的要求,虽然我退出的时候有一个新的请求,previous请求。

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.

这是我的jQuery code:

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;
    }
}

我的HTML:

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:

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
    }               
}

当我点击学校形象还需要一段时间(完成第一个请求),它与家庭形象的要求进行,即使我用request.abort前点击后在回家的图像()。如果我不使用request.abort()花费的时间是相同的,并给了我一个超时错误,然后处理家里图像请求。我如何使它忽略了previous Ajax请求很快,这样它会很快地进入到新的请求,并对其进行处理。

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.

推荐答案

我想通了这个问题:的长轮询锁定了其他AJAX调用 ... - PHP锁定一个给定的会话,直到页面加载完成,以便第二次Ajax调用无法通过。你必须通过调用 session_write_close()释放锁;

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();

这篇关于长轮询与服务器端的jQuery / AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆