Hey there, I'm currently working on a JavaScript game and facing an issue. I want to execute a function when an if statement is true, but the function keeps running repeatedly. I need it to run only once.
This is my current code:
//JavaScript
setInterval(function () {
var characterTop = parseInt(
window.getComputedStyle(character).getPropertyValue("top")
)
var characterLeft = parseInt(
window.getComputedStyle(character).getPropertyValue("left")
)
if (characterTop > 200 && characterLeft > 700) {
document.getElementById("win").classList.toggle("active")
{stopMoveLeft();}
{stopMoveRight();}
{stopMoveDown();}
{stopMoveUp();}
}
}, 50);
/*CSS*/
#win {
height: 100%;
width: 100%;
background-color: black;
position: fixed;
top: 600px;
transition: all 500ms linear;
}
#win.active {
top: 0px;
}
<!--HTML-->
<div id="win"></div>
Is there a way to keep checking the condition but ensure that the function executes only once?