添加到ul列表时的Javascript无限循环

添加到ul列表时的Javascript无限循环,javascript,Javascript,我正在制作一个蛇类游戏,我正在尝试为列表中当前所有玩的蛇添加一个排行榜 这是我正在使用的js函数 function UpdateScore(snakes) //Updates the scores { $("LeaderBoardList").empty(); for (i in snakes) { //For each instance of snake var CurrentSnake = snakes[i]; //Make that the curren

我正在制作一个蛇类游戏,我正在尝试为列表中当前所有玩的蛇添加一个排行榜

这是我正在使用的js函数

function UpdateScore(snakes) //Updates the scores
{
    $("LeaderBoardList").empty();

    for (i in snakes) { //For each instance of snake
         var CurrentSnake = snakes[i]; //Make that the current snake
    var CurrentPlayerID = CurrentSnake.PlayerID; //make their id the current ID

    if (CurrentPlayerID == PlayerID) //Current player has special css for displaying score
    {
        var thisPlayerName = "<h4 class='PlayerScore'>Player " + CurrentPlayerID + "</h3>";
    } else //Different class for other players
    {
        var thisPlayerName = "<h4 class='EnemyScore'>Player " + CurrentPlayerID + "</h3>";
    }

    thisPlayerName += "<h4 class = 'Scores'> Kills: " + CurrentSnake.Kills + ' Score: ' + CurrentSnake.Score + '</h4>'

    $('#LeaderBoardList').append(thisPlayerName); //Show a line for each player

}
}

我最初认为这是因为这个函数被调用了很多次,调用速度非常快,所以我添加了代码来清除列表,但这没有帮助

我刚刚用我的游戏测试了这个,它运行了一个无限循环,图

提前感谢

更改

thisPlayerName += "<h4 class = 'Scores'> Kills: " + CurrentSnake.Kills + ' Score: ' + CurrentSnake.Score + '</h4>'

将+从中移除+=

thisPlayerName = "<h4 class = 'Scores'> Kills: " + CurrentSnake.Kills + ' Score: ' + CurrentSnake.Score + '</h4>'