I'd like to set a timeout to make it appear as if a card is being thrown to the right when the user presses the right arrow key. The function is a part of an object. Ideally, i'd like to write something like this:
for (const i of Array(5).keys()) {
let posX = i;
**sleep(10);**
this.topCard.style.transform =
'translateX(' + posX + 'px) translateY(' + posY + 'px) rotate(' + deg + 'deg) rotateY(0deg) scale(1)';
}
Changed my code to this after reading up on how to set a timeout in javascript
throwRight() {
let posX = 10;
let posY = -300;
let deg = 5;
for (const i of Array(5).keys()) {
setTimeout(function() {
let posX = i;
console.log(i)
this.topCard.style.transform =
'translateX(' + posX + 'px) translateY(' + posY + 'px) rotate(' + deg + 'deg) rotateY(0deg) scale(1)';
},10)
}
but now it's complaining that "this" is not defined inside my method. I have the feeling I'm going about this in the wrong way. What's a good way to "do something slowly" in Javascript?