Javascript 函数仅每隔一段时间执行一次
Javascript function only executes every other time
我正在做一个猜字母游戏,我在底部有一个记分牌,显示玩家在输掉游戏之前还剩下多少生命。我有一个屏幕键盘,用户可以点击它来选择他们想猜的字母。
一切正常,除了移除玩家的'lives'。我的生活是有序列表中的图像。每次单击按钮时,都会有一个函数检查按下的按钮是否与短语中的字母相匹配。如果没有,它需要从有序列表中删除一条生命,并将错过的计数加 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]);
的调用将删除第一个节点。在你的例子中,这不一定是一个元素,也可以是一个只包含白色 space 的文本节点。这可能就是为什么只有每个第二个调用看起来都在做某事的原因。另一个是去除白色space。我已更改代码以显式删除第一个 "heart" 节点。当然,还有 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>
只需像这样更新您的 changeScore()
方法:
function changeScore() {
let scoreboardList = document.querySelector('ol');
// Replace this
// scoreboardList.removeChild(scoreboardList.childNodes[0]);
// By this 'EDIT'
scoreboardList.removeChild(scoreboardList.querySelector('li:first-child'));
}
每次答案为假时都会移除一颗心。
我正在做一个猜字母游戏,我在底部有一个记分牌,显示玩家在输掉游戏之前还剩下多少生命。我有一个屏幕键盘,用户可以点击它来选择他们想猜的字母。
一切正常,除了移除玩家的'lives'。我的生活是有序列表中的图像。每次单击按钮时,都会有一个函数检查按下的按钮是否与短语中的字母相匹配。如果没有,它需要从有序列表中删除一条生命,并将错过的计数加 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]);
的调用将删除第一个节点。在你的例子中,这不一定是一个元素,也可以是一个只包含白色 space 的文本节点。这可能就是为什么只有每个第二个调用看起来都在做某事的原因。另一个是去除白色space。我已更改代码以显式删除第一个 "heart" 节点。当然,还有 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>
只需像这样更新您的 changeScore()
方法:
function changeScore() {
let scoreboardList = document.querySelector('ol');
// Replace this
// scoreboardList.removeChild(scoreboardList.childNodes[0]);
// By this 'EDIT'
scoreboardList.removeChild(scoreboardList.querySelector('li:first-child'));
}
每次答案为假时都会移除一颗心。