-1
I’m making a code to make a typing game: by typing the word correctly the user gains an extra time in the time being decreased and gains 1 point, and loses if he reaches 0s.
For example: if you get the word right when it’s in 7s, that time goes to 9 seconds.
But when the word hit event is triggered, I clear the interval and Seto the variable corresponding to the interval to call the function again, now with the seconds to add to the time (in case 2s)but the game starts to behave strangely and does not do what the function should continue to do, so I think this function is not being called.
The function to setInterval
can only be called once by it? Or else when clearing the interval has no way to continue it?
var word = document.getElementById("word");
var wordTyped = document.getElementById("text");
var time = document.getElementById("time");
var score = document.getElementById("score");
var endGame = document.querySelector(".end-game-container");
const settingsBtn = document.getElementById("settings-btn");
const difficultySettings = document.getElementById("settings");
var secondsInterval = setInterval(updateTime, 1000, 0);
var currentTime = Number(
time.textContent.substring(0, time.textContent.length - 1)
);
const words = [
"bad",
"view",
"world",
"release",
"caracteristic",
"grass",
"homeland",
"building",
"juice",
"illness",
];
//FUNÇÃO QUE GERA UMA PALAVRA NOVA NA TELA AO ACERTAR A ANTERIOR
function randomWord(secondsToAdd) {
if (secondsToAdd > 0) {
clearInterval(secondsInterval);
secondsInterval = setInterval(updateTime, 1000, secondsToAdd);//PARTE QUE O CÓDIGO PARA DE FUNCIONAR DE FORMA NORMAL JÁ QUE OS SEGUNDOS A ADICIONAR É MAIOR QUE 0 POIS É CHAMADA QUANDO ACERTOU A PALAVRA
}
let selectedWord = words[Math.floor(Math.random() * words.length)];
word.textContent = selectedWord;
}
function lostTheGame() {
endGame.style.display = "flex";
endGame.innerHTML = `<h1>Time ran out</h1> <p> Your final score is ${score.innerHTML} <button onclick=reload() > Reload </button>`;
}
function reload() {
wordTyped.value = "";
time.textContent = "10s";
score.textContent = "0";
randomWord(0);
updateTime(0);
endGame.style.display = "none";
}
//FUNÇÃO CORRESPONDENTE AO INTERVALO
function updateTime(secondsToAdd) {
time.textContent = `${currentTime}s`;
if (currentTime == 0) {
clearInterval(secondsInterval);
lostTheGame();
}
currentTime += secondsToAdd;
currentTime--;
}
wordTyped.addEventListener("keyup", () => {
if (wordTyped.value == word.textContent) {
randomWord(2);
wordTyped.value = "";
score.innerHTML = +score.innerHTML + 1;
}
});
randomWord(0);
settingsBtn.addEventListener("click", () => {
difficultySettings.classList.toggle("hide");
});
* {
box-sizing: border-box;
}
body {
background-color: #2c3e50;
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
button {
cursor: pointer;
font-size: 14px;
border-radius: 4px;
padding: 5px 15px;
}
select {
width: 200px;
padding: 5px;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
border-radius: 0;
background-color: #a7c5e3;
}
select:focus,
button:focus {
outline: 0;
}
.settings-btn {
position: absolute;
bottom: 30px;
left: 30px;
}
.settings {
position: absolute;
top: 0;
left: 0;
width: 100%;
background-color: rgba(0, 0, 0, 0.3);
height: 70px;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
transform: translateY(0);
transition: transform 0.3s ease-in-out;
}
.settings.hide {
transform: translateY(-100%);
}
.container {
background-color: #34495e;
padding: 20px;
border-radius: 4px;
box-shadow: 0 3px 5px rgba(0, 0, 0, 0.3);
color: #fff;
position: relative;
text-align: center;
width: 500px;
}
h2 {
background-color: rgba(0, 0, 0, 0.3);
padding: 8px;
border-radius: 4px;
margin: 0 0 40px;
}
h1 {
margin: 0;
}
input {
border: 0;
border-radius: 4px;
font-size: 14px;
width: 300px;
padding: 12px 20px;
margin-top: 10px;
}
.score-container {
position: absolute;
top: 60px;
right: 20px;
}
.time-container {
position: absolute;
top: 60px;
left: 20px;
}
.end-game-container {
background-color: inherit;
display: none;
align-items: center;
justify-content: center;
flex-direction: column;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css"
integrity="sha256-+N4/V/SbAFiW1MPBCXnfnP9QSN3+Keu+NlB+0ev/YKQ="
crossorigin="anonymous"
/>
<link rel="stylesheet" href="style.css" />
<title>Speed Typer</title>
</head>
<body>
<button id="settings-btn" class="settings-btn">
<i class="fas fa-cog"></i>
</button>
<div id="settings" class="settings">
<form id="settings-form">
<div>
<label for="difficulty">Difficulty</label>
<select id="difficulty">
<option value="easy">Easy</option>
<option value="medium">Medium</option>
<option value="hard">Hard</option>
</select>
</div>
</form>
</div>
<div class="container">
<h2> Speed Typer </h2>
<small>Type the following:</small>
<h1 id="word"></h1>
<input
type="text"
id="text"
autocomplete="off"
placeholder="Type the word here..."
/>
<p class="time-container">Time left: <span id="time">10s</span></p>
<p class="score-container">Score: <span id="score">0</span></p>
<div id="end-game-container" class="end-game-container"></div>
</div>
<script src="script.js"></script>
</body>
</html>