简体   繁体   English

Javascript setTimeout scope 错误或错误?

[英]Javascript setTimeout scope error or bug?

I need to make a pexeso game as my homework to school.我需要制作一个 pexeso 游戏作为我的学校作业。 I have basically implemented the whole game, but I am stuck at setTimeout issue.我基本上已经实现了整个游戏,但我陷入了 setTimeout 问题。

This is the main logic of the game:这是游戏的主要逻辑:

let firstCard = null;
let secondCard = null;
let points = 0;
let turnedCards = 0;

const createCard = (cityName) => {
    const card = document.createElement('div');
    card.classList.add('card');
    card.innerText = cityName;
    card.addEventListener('click', () => {
        if (firstCard == null) {
            firstCard = card;
            card.classList.add('revealed');
        } else if (secondCard == null) {
            secondCard = card;
            card.classList.add('revealed');
            if (firstCard.innerText == secondCard.innerText && firstCard != secondCard && !firstCard.classList.contains('final') && !secondCard.classList.contains('final')) {
                updateScore();
                firstCard.classList.add('final');
                secondCard.classList.add('final');
            } else {
                setTimeout(() => {
                    if (!firstCard.classList.contains('final')) {
                        firstCard.classList.remove('revealed');
                    }
                    if (!secondCard.classList.contains('final')) {
                        secondCard.classList.remove('revealed');
                    }

                },2000);
            }


            firstCard = null;
            secondCard = null;
        }
    });
    gameArea.appendChild(card);
}

the setTimeout method is there to provide you some time to see turned cards. setTimeout 方法可以为您提供一些时间来查看翻牌。 Now, when I start this game, I always get现在,当我开始这个游戏时,我总是得到

main.js:38 Uncaught TypeError: Cannot read properties of null (reading 'classList')

Which come from the firstCard.classList and secondCard.classList in the setTimeout method.其中来自 setTimeout 方法中的 firstCard.classList 和 secondCard.classList。 I mean, how can firstCard and secondCard be null, if i explicitly assigned value to them just before the method executed?我的意思是,如果我在方法执行之前明确地为它们赋值,那么 firstCard 和 secondCard 怎么能是 null?

Any help appreciated:)任何帮助表示赞赏:)

As pointed out in a comment, the error is most likely due by the assignments正如评论中指出的那样,错误很可能是由作业引起的

firstCard = null;
secondCard = null;

which run before the setTimeout callback.setTimeout回调之前运行。 In fact, the callback runs with 2000ms delay, so most likely after that assignment has been executed.事实上,回调以 2000 毫秒的延迟运行,因此很可能是在执行该分配之后。

I am not sure if this will break the logic of the game, but a solution might be to move these assignments inside the setTimeout callback itself (as well as in the "then" branch of the larger if block).我不确定这是否会破坏游戏的逻辑,但解决方案可能是将这些分配移动到setTimeout回调本身(以及更大的if块的“then”分支中)。

if (firstCard.innerText == secondCard.innerText && firstCard != secondCard && !firstCard.classList.contains('final') && !secondCard.classList.contains('final')) {
  updateScore();
  firstCard.classList.add('final');
  secondCard.classList.add('final');
  firstCard = null;
  secondCard = null;
} else {
  setTimeout(() => {
    if (!firstCard.classList.contains('final')) {
        firstCard.classList.remove('revealed');
    }
    if (!secondCard.classList.contains('final')) {
        secondCard.classList.remove('revealed');
    }
    firstCard = null;
    secondCard = null;
  },2000);
}

Mozilla's guide to setTimeout might be a good resource to help with this issue: https://developer.mozilla.org/en-US/docs/Web/API/setTimeout Mozilla 的setTimeout指南可能是帮助解决此问题的好资源: https://developer.mozilla.org/en-US/docs/Web/API/setTimeout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM