求和变量,并不断显示和更新它


Sum variable and keep showing and updating it

我有这个函数,它是我游戏的一部分,它将数据发送到getScore.php。在这个php中,我有一些代码,可以向我发回玩家输入的每个标签的分数,比如"100"50"或"10"。现在我想抓住这些分数并将其相加,并不断向玩家显示他的分数(更新),直到游戏结束。

function showScore() {
    $.ajax({
        type: "GET",
        url: "getScore.php",
        data:{
            tag: $("#tag").val(),
            timecode: curTime,
            filename: getFileName(),
            gameid: gameID()
        }
    }).done(function( msg )
    {
    show3(msg);
    });
    return false; 
};

最后一个函数是我显示分数的地方,但现在我想把它改为sum和show。我该怎么做?!

var score;
function show3(string)
{
document.getElementById('myScore').innerHTML = string;
return string;
}

我猜您想将从myScore.php获得的分数添加到score并显示它。parseInt()函数应该可以做到这一点:

function show3(msg)
{
score = score+parseInt(msg);
document.getElementById('myScore').innerHTML = score;
return score;
}

首先,命名var和函数参数score会让您感到困惑。其次,您没有添加从getScore.php获得的分数,而是替换它。