Looking at the code snippet below, it seems to be running at a frame rate of 60 frames per second. If any button is clicked, the variable isjump will be set to true and the following code will start executing. The goal here is to exit the jump() function after 1 second. I have tried using setTimeout as shown below, but the lines inside the setTimeout are running indefinitely. Can someone please help?
My ultimate objective is to exit the jump function after 1 second.
function jump() {
if (isjump) {
player.posx +=10;
console.log("jumping"); //this line should stop after 1 second
setTimeout(function () {
isjump = false;
console.log("stopped"); //however, this line keeps on running
}, 1000);
}
}
I have also attempted using clearTimeout without success.
var timeoutHandle = window.setTimeout(function () {
isjump = false;
console.log("stopped");
clearTimeout(timeoutHandle);
}, 500);
Edit: Just to clarify for those who may think the jump function is called continuously - yes, it is indeed called continuously. The game character position is updated for every frame.