0

我正在创建一个简单的井字游戏,并且我有一个事件处理程序可以触发代码中的许多函数。问题是,当游戏结束时,我想禁用该事件处理程序,以便游戏可以结束并且用户无法与之交互。我已经调查过了,解绑/绑定、开/关/道具/属性归零不起作用。当满足条件时,还有其他方法可以禁用此事件处理程序吗?我想禁用 .one('click') 处理程序而不是开始父处理程序。

//Creates the variables needed to be manipulated later
$(document).ready(function () {
    var X = 'X';
    var O = 'O';
    var currentPlayer;
    var turnCount = 0;
    var xMoves = [];
    var oMoves = [];
    var cellTracker;
    var winAlert;
    var winConditions = [
        ['c1', 'c2', 'c3'],
        ['c4', 'c5', 'c6'],
        ['c7', 'c8', 'c9'],
        ['c1', 'c4', 'c7'],
        ['c2', 'c5', 'c8'],
        ['c3', 'c6', 'c9'],
        ['c1', 'c5', 'c9'],
        ['c3', 'c5', 'c7']
    ];

    /*Set's the current player to X if turnCount is odd
    And to O if turnCount is even*/
    var setCurrentPlayer = function () {
        if (turnCount % 2 === 0) {
            currentPlayer = O;
        } else {
            currentPlayer = X;
        }
    };

    //Pushes cellTracker's value to the curent player's move variable
    var storeMoves = function () {
        if (currentPlayer === X) {
            xMoves.push(cellTracker);
        } else if (currentPlayer === O) {
            oMoves.push(cellTracker);
        }
    };

    //Compares players moves with the winConditions to determine a winner
    var determineWin = function (pMoves) {
        for (var i = 0; i < winConditions.length; i++) {
            if (winConditions[i].length > pMoves.length) {
                continue;
            }
            for (var j = 0; j < winConditions[i].length; j++) {
                winAlert = false;
                for (var k = 0; k < pMoves.length; k++) {
                    if (pMoves[k] === winConditions[i][j]) {
                        winAlert = true;
                        break;
                    }

                }
                if (!winAlert) break;
            }
             if (winAlert) {
                alert(currentPlayer + " wins!");
                break;
            }
        }
    };

    /*Place's an X or an O when a cell is clicked depending
    on the current player, and updates the turnCount*/
    $('td').one('click', function () {
        turnCount += 1;
        setCurrentPlayer();
        $(this).text(currentPlayer);
        cellTracker = $(this).attr('id');
        storeMoves();
        determineWin(currentPlayer == 'X' ? xMoves : oMoves);
        if (turnCount === 9 && winAlert === false) {
            alert("Tie game!");
        }
        console.log(turnCount, xMoves, oMoves, winAlert);
    });
});
4

3 回答 3

1

尝试将其定义为函数:

var handler = function () {
        turnCount += 1;
        setCurrentPlayer();
        $(this).text(currentPlayer);
        cellTracker = $(this).attr('id');
        storeMoves();
        determineWin(currentPlayer == 'X' ? xMoves : oMoves);
        if (turnCount === 9 && winAlert === false) {
            alert("Tie game!");
        }
        console.log(turnCount, xMoves, oMoves, winAlert);
    });

然后附上它:

$('td').one('click', handler);

每当你想删除它时,你可以去:

$('td').off('click', handler);

编辑:您需要在将其取下之前定义处理程序变量,但这应该是合理的。只需在顶部定义它并在调用之前设置它one,它应该可用于代码的任何其他部分。

编辑:看看你的小提琴。有关可行的解决方案,请参阅http://jsfiddle.net/c4496/1/。就像我之前说的,$.off总是有效的:你的用法是错误的。要执行您想要的操作,您必须$.off在检测到要结束游戏时调用该调用,而不是在底部编写代码并期望它在您更改变量值时为您神奇地调用。如果您想要这种行为,请查看 Knockout 或 AngularJS。

于 2013-06-10T05:57:39.890 回答
0

使用 .off() 和事件名称间距

$('td').one('click.mygame', function () {
    turnCount += 1;
    setCurrentPlayer();
    $(this).text(currentPlayer);
    cellTracker = $(this).attr('id');
    storeMoves();
    determineWin(currentPlayer == 'X' ? xMoves : oMoves);
    if (turnCount === 9 && winAlert === false) {
        alert("Tie game!");
    }

    if(condition){ //like ( winAlert === true  || turnCount === 9)
        $('td').off('click.mygame')
    }

    console.log(turnCount, xMoves, oMoves, winAlert);
});

演示:小提琴

于 2013-06-10T06:00:09.883 回答
0

您是否尝试过禁用发生单击事件的元素?例如,如果你想禁止点击你的td元素,你可以使用:

$('td').prop("disabled",true);

当您想重新启用它时,只需使用:

$('td').prop("disabled",false);

将 prop 或 attr 方法归零无济于事。使用上面的方法。希望能帮助到你 !

于 2013-06-10T05:54:32.763 回答