第一次按键后会稍有停顿。
这是日志:

[00.000ms] keypress   -- first keypress
[496.00ms] keypress   -- second keypress after 496 ms
[39.000ms] keypress   -- every other keypress after about 20-40 ms.
[41.000ms] keypress
[21.000ms] keypress
[39.000ms] keypress
...

So there's a pause of half a second after a first keypress, the rest is fired after 20-40 ms.

This is the code to grab the keypresses:

// If we're using the webkit engine, capture the keydowns
if(navigator.userAgent.indexOf("WebKit") != -1){
    window.onkeydown = onKeyPress;
} else{
    // If we're using gecko, capture the keypress
    if(navigator.userAgent.indexOf("Gecko") != -1){
        window.onkeypress = onKeyPress;
    }
}




onKeyPress函数执行的第一件事是调试输出(来自上面的日志)

有人知道为什么会有暂停吗?
就像在Windows或Linux上一样; Firefox或Chrome。

最佳答案

我认为“真实游戏”可以访问的直播时间要短得多,而Javascript受操作系统自动重复按键限制的约束。

我解决的方法是使用onKeyDownonKeyUp创建解决方法:

因此,当触发onKeyDown事件时,请继续重复该过程,直到触发onKeyUp事件为止。

关于javascript - 第一次按键后暂停,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3466852/

10-12 07:29