gpt4 book ai didi

Javascript 函数仅每隔一段时间执行一次

转载 作者:行者123 更新时间:2023-11-30 19:43:05 26 4
gpt4 key购买 nike

我正在开发一个字母猜谜游戏,我在底部有一个记分牌,显示玩家在输掉游戏之前还剩下多少生命。我有一个屏幕键盘,用户可以点击它来选择他们想猜的字母。

除了移除玩家的“生命”外,一切正常。我的生活是有序列表中的图像。每次单击按钮时,都会有一个函数检查按下的按钮是否与短语中的字母相匹配。如果没有,它需要从有序列表中删除一条生命,并将错过的计数加 1。

在每次错误的按钮按下时将错过的计数加 1,但移除心形仅对其他所有按钮都有效,我不确定为什么。任何帮助将不胜感激。

function checkLetter(clickedButton) {
const letters = document.getElementsByClassName('letter');
let letterFound = null;
for (let i = 0; i < letters.length; i += 1) {
if (clickedButton === letters[i].textContent) {
letters[i].classList.add('show');
letterFound = true;
}
}
return letterFound;
}

function changeScore() {
let scoreboardList = document.querySelector('ol');
scoreboardList.removeChild(scoreboardList.childNodes[0]);
}

qwerty.addEventListener('click', (e) => {
const button = e.target;
const buttonText = button.textContent;
checkLetter(buttonText);
if (checkLetter(buttonText) == null) {
missed += 1;
changeScore();
console.log(missed);
} else {
button.classList.add('chosen');
}
});
   

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Wheel of Success!</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/styles.css" rel="stylesheet">
</head>
<body>
<div class="main-container">
<div id="overlay" class="start">
<h2 class="title">Wheel of Success</h2>
<a class="btn__reset">Start Game</a>
</div>

<div id="banner" class="section">
<h2 class="header">Wheel of Success</h2>
</div>

<div id="phrase" class="section">
<ul></ul>
</div>
<div id="qwerty" class="section">
<div class="keyrow">
<button>q</button><button>w</button><button>e</button><button>r</button><button>t</button><button>y</button><button>u</button><button>i</button><button>o</button><button>p</button>
</div>
<div class="keyrow">
<button>a</button><button>s</button><button>d</button><button>f</button><button>g</button><button>h</button><button>j</button><button>k</button><button>l</button>
</div>
<div class="keyrow">
<button>z</button><button>x</button><button>c</button><button>v</button><button>b</button><button>n</button><button>m</button>
</div>
</div>

<div id="scoreboard" class="section">
<ol>
<li class="tries"><img src="images/liveHeart.png" height="35px" width="30px"></li>
<li class="tries"><img src="images/liveHeart.png" height="35px" width="30px"></li>
<li class="tries"><img src="images/liveHeart.png" height="35px" width="30px"></li>
<li class="tries"><img src="images/liveHeart.png" height="35px" width="30px"></li>
<li class="tries"><img src="images/liveHeart.png" height="35px" width="30px"></li>
</ol>
</div>
</div>
<script type="text/javascript" src="js/app.js"></script>
</body>
</html>

最佳答案

据我所知,您对 scoreboardList.removeChild(scoreboardList.childNodes[0]); 的调用将删除第一个节点。在您的情况下,这不一定是一个元素,但也可以是仅包含空格的文本节点。这可能就是为什么只有每个第二个调用看起来都在做某事的原因。另一个是删除空白。我更改了代码以明确删除第一个“心脏”节点。当然,还有 Lewis 在他的评论中提到的内容:不要调用 checkLetter 两次。直接在 if 中使用即可。

let missed = 0;

function changeScore() {
let scoreboardList = document.querySelector('ol');

// !!!!!! These two lines are the important changes:
let heart = scoreboardList.querySelector('li:first-child'); // find first heart item
scoreboardList.removeChild(heart); // remove the heart
}

document.addEventListener('click', (e) => {
//const button = e.target;
//const buttonText = button.textContent;
//if (checkLetter(buttonText) == null) {
missed += 1;
changeScore();
console.log(missed);
//} else {
// button.classList.add('chosen');
//}
});
<div id="scoreboard" class="section">
<ol>
<li class="tries"><img src="http://pngimg.com/uploads/heart/heart_PNG51341.png" height="35px" width="30px"></li>
<li class="tries"><img src="http://pngimg.com/uploads/heart/heart_PNG51341.png" height="35px" width="30px"></li>
<li class="tries"><img src="http://pngimg.com/uploads/heart/heart_PNG51341.png" height="35px" width="30px"></li>
<li class="tries"><img src="http://pngimg.com/uploads/heart/heart_PNG51341.png" height="35px" width="30px"></li>
<li class="tries"><img src="http://pngimg.com/uploads/heart/heart_PNG51341.png" height="35px" width="30px"></li>
</ol>
</div>

关于Javascript 函数仅每隔一段时间执行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55201718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com