I am currently programing an app as a project. While doing so I just got into Javascript yesterday and I am having an issue to stop my count.
What is my program doing:
You can see an image, which you have to click on. With every click the count gets +1. While doing so, a time of 30sec is running down and showing on the screen as well.
What do I want to reach:
I got everything working so far. The timer is working perfectly, the count is working while I am clicking on the image. But what I can’t get to work is that the image should not be click-able anymore after the timer hits 00:00 and the count should stay at the last reached number, so I can put it into a highscore list.
It’s especially about this part:
function blobPress() {
if (timeLeft > 0) {
count++;
document.getElementById("count").innerHTML = count;
} else {
blob.style.display = "block";
}
}
let alarm = new Audio('alarm.mp3');
let finish = new Audio('finish.mp3');
let timerStarted = false;
let count = 0;
function startTimer() {
if (!timerStarted) {
let startTime = new Date().getTime();
let thirtySeconds = 1000 * 30;
let endTime = startTime + thirtySeconds;
alarm.play();
let refreshInterval = setInterval(function() {
let timeLeft = endTime - new Date().getTime();
if (timeLeft > 0) {
let minutes = timeLeft / (1000 * 60);
minutes = Math.floor(minutes);
let seconds = (timeLeft / 1000) % 60;
seconds = Math.round(seconds);
seconds = ('0' + seconds).slice(-2);
let text = '0' + minutes + ' : ' + seconds;
timer.innerHTML = text;
} else {
finish.play();
timer.innerHTML = '00 : 00';
clearInterval(refreshInterval);
}
}, 1000);
timerStarted = true;
}
}
function showCount() {
if (timerStarted) {
document.getElementById('count').style.color = "white";
} else {
document.getElementById('count').style.color = "#262626";
}
}
function hideButton() {
if (button.style.display === "none") {
button.style.display = "block";
} else {
button.style.display = "none";
}
}
function blobPress() {
if (timeLeft > 0) {
count++;
document.getElementById("count").innerHTML = count;
} else {
blob.style.display = "block";
}
}
body {background-color: #262626;}
.background-dark {
background-color: #262626;
}
.center {
display: flex;
justify-content: center;
}
.margin-top {
margin-top: 60px;
}
.timer {
font-size: 80px;
color: white;
font-family: 'Roboto', sans-serif;
}
.count {
font-size: 50px;
color: #262626;
font-family: 'Roboto', sans-serif;
}
<div>
<img src="img/menu.png">
</div>
<div class="center margin-top count" id="count">0</div>
<div class="center margin-top">
<img id="blob" onclick="blobPress()" src="img/blob.png" style="transform: scale(0.9);">
</div>
<div class="center margin-top timer" id="timer">00 : 30</div>
<div class="center margin-top">
<img id="button" onclick="startTimer(); showCount(); hideButton()" src="img/button.png" style="transform: scale(0.8);">
</div>
2
Answers
The issues were:
timeLeft
was not defined insideblobPress()
.count
was not handled correctly.I fixed the issues by doing the following changes:
onclick
from#blob
.timeLeft
toblobPress(timeLeft)
.parseTimeToSeconds(timeStr)
to returntimeLeft
ininteger
as when you get it byinnerHTML
, it returnsstring
.I also added
alt
attributes to theimages
. Rest of yourcode
is untouched.It is generally not a good idea to use inline event handlers. Use
addEventListener
to handle events within the scripting.Muhammad Aleem Hashmi already explained the issues with your code.
I took the liberty to create a timer factory. Maybe its useful for your project.
To demonstrate its use here is a minimal reproducable example based on your code. The handling uses event delegation.