I'm playing with the Gamepad API - in particular the axes using the joysticks on a controller. The position of these updates a lot and often - as such, the event that I'm listening for (movement on the sticks) also happens a lot. Is there any way to limit it happening to, say, 25 times a second in order to reduce lag?
You can't limit the rate at which JavaScript events are triggered, but your event handler could opt to do nothing on some calls. Here is an example using mousemove
(I don't know which Gamepad API you're talking about):
var lastMove = 0;
document.addEventListener('mousemove', function() {
// do nothing if last move was less than 40 ms ago
if(Date.now() - lastMove > 40) {
// Do stuff
lastMove = Date.now();
}
});
http://jsfiddle.net/jk3Qh/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With