单击记忆游戏的第一张牌时开始计时

Starting timer when clicking first card of memory game

好的,我正在尝试结束一个项目,唯一让我退缩的是他们要求计时器在点击比赛游戏时开始计时。计时器在 HTML 文件加载时启动,这不是项目想要的,我尝试了一些方法但最终冻结了游戏。我希望计时器能够在单击卡片时启动。

var open = [];
var matched = 0;
var moveCounter = 0;
var numStars = 3;
var timer = {
    seconds: 0,
    minutes: 0,
    clearTime: -1
};

//Start timer
    var startTimer = function () {
        if (timer.seconds === 59) {
            timer.minutes++;
            timer.seconds = 0;
        } else {
            timer.seconds++;
    };


    // Ensure that single digit seconds are preceded with a 0
    var formattedSec = "0";
        if (timer.seconds < 10) {
            formattedSec += timer.seconds
        } else {
            formattedSec = String(timer.seconds);
        }

        var time = String(timer.minutes) + ":" + formattedSec;
        $(".timer").text(time);
    };

这是点击卡片的代码。我试图在其中包含一个 startTimer 代码,但不起作用。

var onClick = function() {
    if (isValid( $(this) )) {

        if (open.length === 0) {
            openCard( $(this) );

        } else if (open.length === 1) {
            openCard( $(this) );
            moveCounter++;
            updateMoveCounter();

            if (checkMatch()) {
                setTimeout(setMatch, 300);

            } else {
                setTimeout(resetOpen, 700);

            }
        }
    }
};

我在 HTML 文件中使用的 class 代码

<span class="timer">0:00</span>

试试这个:https://codepen.io/anon/pen/boWQbe

您需要做的就是从页面加载时发生的函数中删除 resetTimer() 调用,然后检查 onClick(卡片)以查看计时器是否已启动然而。 timer.seconds == 0 && timer.minutes == 0.