+ 2

How to add cooldown in javascript?

Hey, i got some code and i need to add a cooldown to it.. I have no idea how to get it to work i tried to add , 1000); after if (Keys.pressed["ArrowLeft"]) { x = Math.max(0, hero.x - 1); so it would cooldown for 1000 ms, but it doenst work.. " function update() { let x = hero.x, y = hero.y; if (Keys.pressed["ArrowLeft"]) { x = Math.max(0, hero.x - 1); } if (Keys.pressed["ArrowRight"]) { x = Math.min(w - 1, hero.x + 1); } if (Keys.pressed["ArrowUp"]) { y = Math.max(0, hero.y - 1); } if (Keys.pressed["ArrowDown"]) { y = Math.min(h - 1, hero.y + 1); } " If someone could help me with this, would be amazing!

6th Jul 2019, 11:02 AM
mine
mine - avatar
1 Answer
+ 2
Hi. I don't know how you handle your events, but if you are using boolean values, then it could be something like this: function keydown(e) { if (e.keyCode == 37) setTimeout(() => { Keys.pressed["ArrowLeft"] = true; }, 1000); ... }
6th Jul 2019, 11:09 AM
Airree
Airree - avatar