I have a setTimeout defined inside of a function that controls the player's respawn (i am creating a game):
var player = {
...
death:(function() {
this.alive = false;
Console.log("death!");
var timer3 = setTimeout((function() {
this.alive = true;
Console.log("alive!");
}),3000);
}),
...
}
When it executes, I read in the console, "death!" and 3 seconds later "alive!". However, alive is never really set back to true, because if i write player.alive in the console, it returns false. How come i can see "alive!" but the variable is never set back to true?
thisis the anonymous function passed to the setTimeout, you need to assignthisto some other variable and check it later