I am trying to develop a simple turn game in JS, and i need to make those battle interactions to have some delay.
I am using setInverval to automatize everything after the player presses 'Battle!', but when checking if he's still alive for it, my clearInterval doesn't work - the process is still running in the background o_o
const player = {
hp: 0,
state: true
}
const enemy = {
hp: 100,
state: true
}
function checkHp() {
if(player.hp === 0 || enemy.hp === 0){
const start = setInterval(function() {
console.log('Checando turnos...'),
battle()
}, 2000)
return start
} else {
clearInterval(start)
console.log('Você precisa descansar!')
}
}
function battle() {
turn = 0
turn = Math.floor(Math.random () * 2)
console.log(turn)
}
checkHp()