This is my code for one of the players.
var jintoki = {
name: 'jintoki',
hp: 50,
stats: function() {
console.log('Gintoki');
console.log('HP: ' + jintoki.hp);
},
attack: function() {
console.log('Jintoki attacks Hiji and deals ' + attack + ' pts of damage!');
}
};
This is my code for the simulation.
$('#start').on('click', function() {
while(jintoki.hp > 0 && hiji.hp > 0) {
console.log('---------------------');
jintoki.stats();
console.log('*********************'); // displays stats
hiji.stats();
console.log('---------------------');
attack = Math.floor((Math.random() * 10) + 1); // random attack damage
jintoki.attack();
hiji.hp -= attack;
attack = Math.floor((Math.random() * 10) + 1); // random attack damage
hiji.attack();
jintoki.hp -= attack;
if (jintoki.hp <= 0) {
console.log('Jintoki is dead! Hiji wins!!');
};
if (hiji.hp <= 0) {
console.log('Hiji is dead! Gintoki wins!!');
};
}
});
The problem is that when the button with id #start is clicked, the whole simulation runs from start to end. I want it so that the simulation displays the stats, waits a second or so, then displays the first attack, waits a second or so, then displays the next attack. How do I do this? I also tried doing
setTimeout(jintoki.attack(), 1000);
but it doesn't work.