Witam.
Próbuję robić program, gdzie spadają mi wyrazy a ja mam wpisać odpowiedź. Na razie "a".
Niestety po dwóch wpisanych odpowiedziach program się blokuje.
Gdybym zwiększył ilość haseł, to jak zostaną 2 ostatnie, to program się zablokuje.
Jeżeli pozwoliłbym, żeby na ekranie było widoczne więcej haseł niż jedno, to wtedy program się nie zablokuje.
Co to może być?
Zamieszczam kod:
<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> body {
overflow: hidden;
margin: 0;
font-family: Arial, sans-serif;
}
#container {
position: relative;
height: 100vh;
background-color: #f0f0f0;
}
#formContainer {
position: fixed;
bottom: 0;
left: 50%;
transform: translateX(-50%);
text-align: center;
padding: 10px;
background-color: white;
border: 1px solid #ccc;
}
#wordInput {
padding: 5px;
font-size: 16px;
}
#score {
font-size: 20px;
font-weight: bold;
}
.word {
position: absolute;
font-size: 24px;
font-weight: bold;
}
#line {
position: fixed;
bottom: 80px;
left: 0;
width: 100%;
height: 2px;
background-color: #000;
}
#congratulations {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 36px;
font-weight: bold;
display: none;
}
#remainingWords {
font-size: 16px;
font-weight: bold;
margin-left: 10px;
margin-right: 20px;
}
<body onload="document.getElementById('wordInput').focus()" style="background-color: white; color: black;"> <input type="text" id="wordInput" onfocus="this.focus(); this.select()"> <div id="congratulations">Gratulacje!
</div>
let words = [
{ word: 'wyr1', color: 'a' },
{ word: 'wyr2', color: 'a' },
{ word: 'wyr3', color: 'a' },
{ word: 'wyr4', color: 'a' }
];
const container = document.getElementById('container');
const formContainer = document.getElementById('formContainer');
const wordInput = document.getElementById('wordInput');
const scoreElement = document.getElementById('score');
const remainingWordsElement = document.getElementById('remainingWords');
const line = document.getElementById('line');
const congratulations = document.getElementById('congratulations');
let score = 0;
function createWordElement(wordObject) {
const wordElement = document.createElement('div');
wordElement.className = 'word';
wordElement.textContent = wordObject.word;
wordElement.style.left = `${Math.random() * (container.offsetWidth - 100)}px`; //30
container.appendChild(wordElement);
const fallInterval = setInterval(() => {
const rect = wordElement.getBoundingClientRect();
if (rect.bottom < container.offsetHeight) {
wordElement.style.top = `${rect.top + 0.2}px`;
} else {
clearInterval(fallInterval);
wordElement.remove();
updateScore(-1);
updateRemainingWords();
}
const lineRect = line.getBoundingClientRect();
if (rect.bottom >= lineRect.top && rect.top <= lineRect.bottom && rect.left >= lineRect.left && rect.right <= lineRect.right) {
clearInterval(fallInterval);
wordElement.remove();
updateScore(-1);
removeWordFromList(wordObject);
if (words.length === 0) {
showCongratulations();
}
}
}, 5);
wordInput.addEventListener('input', function handleInput() {
if (wordInput.value.toLowerCase() === wordObject.color.toLowerCase()) {
clearInterval(fallInterval);
wordElement.remove();
updateScore(1);
wordInput.value = '';
wordInput.removeEventListener('input', handleInput);
removeWordFromList(wordObject);
updateRemainingWords();
if (words.length === 0) {
showCongratulations();
}
}
});
}
function removeWordFromList(wordObject) {
if (wordObject) {
words = words.filter(word => word !== wordObject);
}
//console.log(words);
}
function updateScore(delta) {
score += delta;
scoreElement.textContent = score;
}
function updateRemainingWords() {
remainingWordsElement.textContent = words.length;
//console.log(words.length);
}
function showCongratulations() {
congratulations.style.display = 'block';
}
function startGame() {
let wordIndex = 0;
setInterval(() => {
// console.log(words.length);
// console.log(wordIndex);
if (words.length > 0) {
const wordObject = words[wordIndex];
createWordElement(wordObject);
wordIndex = (wordIndex + 1) % words.length;
}
}, 5000);
}
startGame();