I'm writing an input control function for a small javascript game. There seems to be a significantly longer delay between the first and second keydown
events than the subsequent events. Consider the following:
let lastPress = 0;
const keydownHandler = (event) => {
console.log(event.keyCode + ', delta time: ' + (Date.now() - lastPress));
lastPress = Date.now();
}
document.addEventListener("keydown", keydownHandler, false);
If I run this and hold the right arrow key down I'll see a listing like this (in formatted version):
Keycode | Delta time |
---|---|
39 | 142129 |
39 | 492 |
39 | 82 |
39 | 87 |
39 | 82 |
39 | 83 |
39 | 82 |
39 | 84 |
39 | 82 |
39 | 86 |
Now, the first delta is obviously huge (since I'm looking at the difference between the previous call to the event handler). What I don't understand is the value 492 ms compared to the 82 - 87 ms on the following calls.
Why is there such a significant gap between the first two events? And is there a way to narrow that gap?