AJAX导致页面无响应

时间:2014-10-14 20:45:29

标签: jquery ajax google-chrome

我在网站上的游戏中运行了以下代码。

function checkGameState(n) {
    $.ajax({
        url: "gamestate.php?c="+player_correct+"&i="+player_incorrect+"&id="+n,
        context: document.body
    }).done(function( data ) {
        checkGameState(n)
        if (getCookie("gid")!=false) {
            obj = jQuery.parseJSON(data);
            player = getCookie('player');
            if (player=='1') {
                if ((opponent_correct<=parseFloat(obj.player2c)) && (opponent_incorrect<=parseFloat(obj.player2i))) {
                    opponent_correct = parseFloat(obj.player2c);
                    opponent_incorrect = parseFloat(obj.player2i);
                }
            } else {
                if ((opponent_correct<=parseFloat(obj.player1c)) && (opponent_incorrect<=parseFloat(obj.player1i))) {
                    opponent_correct = parseFloat(obj.player1c);
                    opponent_incorrect = parseFloat(obj.player1i);
                }
            }
            renderBars();
        }
    });
}

在游戏开始时调用函数checkGameState,之后可以看到递归调用。这个游戏还有一个玩家版本显然不使用此代码。问题是,在玩这款游戏的多人游戏版本时,看似随意的铬会变得没有反应。唯一不同的是上面的AJAX代码。

有谁知道如何阻止此错误发生?

按要求更新renderBars()

function renderBars() {
    //players calculations
    c = player_correct;
    i = player_incorrect;
    s = player_startTime;
    $correct = $(".score-correct");
    $incorrect = $(".score-incorrect");
    t=c+i;
    t=(t==0)?1:t;
    x=5;
    h=0
    if (get_cookie("high"+getLevel())) {
        h=parseFloat(get_cookie("high"+getLevel()));
        $(".levitate .row h5").last().text(h);
        h+=i;
        h=Math.round(h/2);
    }
    while (x<t || x<h) {
        x+=5;
    }
    $(".levitate .row h5").first().text((c*2)-i); 
    //players high score bar calculations
    if (h || h==0) {
        if (h<=x) {
            if (c<h) {
                $(".score-high").css("opacity","1").css("height",(Math.round((h/x)*100*0.9))+"%");
                $(".score-high b").html("<b style='position:relative;top:-40px'>"+h+"</b>")
            } else {
                $(".score-high").css("opacity","0");
            }
        }
    }
    //opponent calculations
    var ox=0;
    if (typeof opponent_correct !== 'undefined') {
        c = opponent_correct;
        i = opponent_incorrect;
        s = player_startTime;
        $ocorrect = $(".score-correct").last();
        $oincorrect = $(".score-incorrect").last();
        t=c+i;
        t=(t==0)?1:t;
        ox=5;
        while (x<t) {
            ox+=5;
        }
    }
    //determine which scale to use
    if (x<=ox) {
        x=ox;
    }
    //change heights of the score bars
    $incorrect.css("height",((Math.round((player_incorrect/(x))*100))*0.9)+"%");
    $correct.css("height",((Math.round((player_correct/(x))*100))*0.9)+"%");
    if (($correct.parent().height()*(((Math.round((player_correct/(x))*100))*0.9)/100))>25) {
        $correct.html("<b>"+player_correct+"</b>")
    } else {
        $correct.html("<b style='position:relative; top:-28px'>"+player_correct+"</b>")
    }
    if (($incorrect.parent().height()*(((Math.round((player_incorrect/(x))*100))*0.9)/100))>25) {
        $incorrect.html("<b>"+player_incorrect+"</b>")
    } else {
        $incorrect.html("<b style='position:relative; top:-28px'>"+player_incorrect+"</b>")
    }
    if (typeof opponent_correct !== 'undefined') {
        $oincorrect.css("height",((Math.round((opponent_incorrect/(x))*100))*0.9)+"%");
        $ocorrect.css("height",((Math.round((opponent_correct/(x))*100))*0.9)+"%");
        if (($ocorrect.parent().height()*(((Math.round((opponent_correct/(x))*100))*0.9)/100))>25) {
            $ocorrect.html("<b>"+opponent_correct+"</b>")
        } else {
            $ocorrect.html("<b style='position:relative; top:-28px'>"+opponent_correct+"</b>")
        }
        if (($oincorrect.parent().height()*(((Math.round((opponent_incorrect/(x))*100))*0.9)/100))>25) {
            $oincorrect.html("<b>"+opponent_incorrect+"</b>")
        } else {
            $oincorrect.html("<b style='position:relative; top:-28px'>"+opponent_incorrect+"</b>")
        }
    }
}

0 个答案:

没有答案
相关问题