删除JavaScript中的keydown延迟? [英] remove keydown delay in JavaScript?
问题描述
好吧,当你按住键盘上的键时,第一次发射后会有1秒的延迟。
您可以继续打开记事本并按住一个键(前'x'),您会在第一次火灾后看到延迟。
然而,我正在尝试使用JavaScript在HTML5 Canvas中开发游戏,并且1秒延迟非常烦人,另外,它会阻止玩家行走动画..
<那么我怎样才能删除JavaScript中令人讨厌的延迟(没有jQuery!)?
我的keydown事件在这种模式下工作 -
document.onkeydown = getKey;
function getKey(e){
switch(e.keyCode){
case 38:// UP
Player.PositionY--;
休息;
案例39:// RIGHT
Player.PositionX ++;
休息;
case 40:// DOWN
Player.PositionY ++;
休息;
案例37:// LEFT
Player.PositionX--;
休息;
}
}
在此先感谢:)
你可以在 keydown
上启动一个事件并在 keyup上停止它
$('#mycanvas')。on('keydown',function(){
$(document).trigger('start');
});
$('#mycanvas')。on('keyup',function(){
$(document).trigger('stop');
});
$(document).on('start',startAnimation);
$(document).on('stop',stopAnimation);
函数startAnimation(e){// start something}
function stopAnimation(e){// stop something}
Well, when you hold a key on your keyboard, after the first fire there is a 1sec delay.
You can go ahead and open notepad and hold a key (e.x 'x') you'll see after the first fire there is a delay.
However, I'm trying to develop a game in HTML5 Canvas with JavaScript, and that 1sec delay is very annoying, Additionally, it stops the player walking animation..
So how can I delete that annoying delay in JavaScript ( no jQuery! ) ??
My keydown event works in this pattern -
document.onkeydown = getKey;
function getKey(e) {
switch(e.keyCode) {
case 38: // UP
Player.PositionY--;
break;
case 39: // RIGHT
Player.PositionX++;
break;
case 40: // DOWN
Player.PositionY++;
break;
case 37: // LEFT
Player.PositionX--;
break;
}
}
Thanks in Advance :)
you could start an event on keydown
and stop it on keyup
$('#mycanvas').on('keydown', function() {
$(document).trigger('start');
});
$('#mycanvas').on('keyup', function() {
$(document).trigger('stop');
});
$(document).on('start', startAnimation);
$(document).on('stop', stopAnimation);
function startAnimation(e) { //start something }
function stopAnimation(e) { //stop something }
这篇关于删除JavaScript中的keydown延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!