I’m currently grappling with a coding challenge and could really use some assistance. I’ve encountered an issue with a specific code snippet I’m working on. The problem I’m facing occurs when I rapidly click the first time. During this initial click, the code behaves incorrectly. However, interestingly, it starts to handle rapid clicks correctly after this initial hiccup. Despite my efforts to troubleshoot, I’ve been unable to resolve the problem, and I’m looking for guidance.
var gameStatus= "game";
var isGameStillRunning= false;
var isGameBusy= false;
$(".cube").on("click", function() {
if (isGameBusy) {
console.log("game is busy")
return;
}
console.log("isGameBusy "+isGameBusy)
if (gameStatus == "game" && !isGameBusy) {
isGameBusy= true;
console.log("isGameBusy "+isGameBusy)
setTimeout(() => {
gameSequence_filler();
console.log(gameSequence);
$("#game_text").html("Level " + level);
gameSequence_Executer ();
}, 1000);
setTimeout (function () {
isGameBusy= false;
}, gameSequence_Executer_Delay*gameSequence.length*1000+100);
}
else if (gameStatus== "player") {
playerSequence_filler(this);
}
});
Here are some details about the issue:
-
Rapid Clicks: The problem primarily manifests when I spam a series of rapid clicks during the first interaction with the code. It seems that the code isn’t reacting as expected during this initial phase.
-
Troubleshooting Efforts: I’ve invested a considerable amount of time attempting to troubleshoot the issue. I’ve reviewed my code, checked event handling, and inspected the initialization process, but my efforts have been in vain thus far also tried to figure it with chatgpt but couldn’t figure it out.
-
Possible Solution: One potential solution I’m considering is shifting the entire game’s start from the first click to the second. While this may serve as a workaround, I believe it’s crucial to address the root cause of the problem.
I would greatly appreciate any insights, suggestions, or tips that the community can offer to help me resolve this issue. If you require additional details about the code or specific sections of the code snippet, please let me know, and I’ll provide them promptly.
Thank you in advance for your assistance!
2
Answers
The problem was that the "gameSequence.length" was initially set to zero, so the delay time was simply zero. the solution is pretty straightforward by adding ternary operator to replace "gameSequence.length" with "1" if it equals to zero
gameSequence_Executer_Delay*(gameSequence.length > 0? gameSequence.length : 1)*1000+100
the old onegameSequence_Executer_Delay*gameSequence.length*1000+100
Your variable
isGameBusy
is intitallyfalse
so it goes through the guardingif
that returns if game is busy. On your first click that means you are setting it totrue
ifgameStatus == "game"
. Then you set a first timeout to appear after some timegameSequence_Executer_Delay*gameSequence.length*1000+100
which sets it tofalse
so that your guardingif
doesn’t stop your code from executing.Your problem seems to arise from this circumstance. I don’t know when
gameStatus == "game"
andgameStatus == "player"
is truthy but this would also play a role for sure form what you have given. The only wayisGameBusy
is set to true is if!isGameBusy
andgameStatus == "game"
and then to set it to false it only happens after thegameSequence_Executer_Delay*gameSequence.length*1000+100
delay which could be the delay you experience on the first click as you say.From what you have given so far though that is the extension to what I can help you with, I hope you will figure out the rest.
Also your
if (gameStatus == "game" && !isGameBusy)
is redundant because of the guardingif (isGameBusy) { return; }
prior to that and can be written as justif (gameStatus == "game")