Javascript 按下钥匙时忽略延迟?
查看其他帖子,没有帮助 我使用asdw在有限的空间内移动对象。我试图弄清楚如何在按下按键时让物体四处移动,而不会在开始时出现短暂的延迟 谢谢Javascript 按下钥匙时忽略延迟?,javascript,jquery,Javascript,Jquery,查看其他帖子,没有帮助 我使用asdw在有限的空间内移动对象。我试图弄清楚如何在按下按键时让物体四处移动,而不会在开始时出现短暂的延迟 谢谢 $(document).ready(function(){ var longitude = 0; var latitude = 0; $('body#sense').keypress(function(){ if(event.which == 65 || event.which == 97){
$(document).ready(function(){
var longitude = 0;
var latitude = 0;
$('body#sense').keypress(function(){
if(event.which == 65 || event.which == 97){
// Left
if(longitude != 0){
longitude = longitude + 10;
$('img#map').css('margin-left', longitude);
}
}
else (event.which == 68 || event.which == 100){
// Right
if(longitude > -200){
longitude = longitude - 10;
$('img#map').css('margin-left', longitude);
}
}
});
});
网页
<body id="sense">
<h2><center>Use your 'asdw' keys to move the map around</center></h2>
<div id="box">
<img id="map" src="http://www.toronto.ca/culture/victoria-square/images/ch0/Victoria-Square-map-t.gif" alt="" />
</div>
</body>
尝试改用
keydown
事件使用keydown事件,然后启动您自己的计时器间隔来控制您自己的重复间隔(忽略系统重复间隔),然后取消keydowp上的计时器。忽略其他关键事件。您可以在setInterval调用中选择自己的重复时间
var timer = null;
function increaseLongitude() {
// your code here
}
function decreaseLongitude() {
// your code here
}
$("#sense").keyDown(function(e) {
if (!timer) {
if (e.which == 65 || e.which == 97) {
timer = setInterval(increaseLongitude, 100);
increaseLongitude();
} else if (e.which == 68 || e.which == 100) {
timer = setInterval(decreaseLongitude, 100);
decreaseLongitude();
}
}
});
$("#sense").keyUp(function(e) {
if (timer) {
clearInterval(timer);
timer = null;
}
});
签出此版本的代码。它可以通过“按键”事件完成。我相信这是理想的做法
嗯,你是什么意思?对象或整个页面的哪个html?对象;我不知道它是如何运作的。在其上下文中显示map和sense id对象希望有帮助;如果不好,我就把地址填进去。明白了。我建议使用动画而不是css。这可能是问题所在。我用css测试了你的代码,我无法阻止延迟。祝你好运另外,使用
$(“#感觉”)
比$(“身体#感觉”)
更好更快,并产生相同的结果。谢谢,但即使在这种显示中,延迟也会发生。真的很酷的工具,谢谢,我会把它插上并修改它,看看会发生什么不太好,但这肯定会帮助我在其他一些东西上朝着一个不同的方向前进。谢谢
var timer = null;
function increaseLongitude() {
// your code here
}
function decreaseLongitude() {
// your code here
}
$("#sense").keyDown(function(e) {
if (!timer) {
if (e.which == 65 || e.which == 97) {
timer = setInterval(increaseLongitude, 100);
increaseLongitude();
} else if (e.which == 68 || e.which == 100) {
timer = setInterval(decreaseLongitude, 100);
decreaseLongitude();
}
}
});
$("#sense").keyUp(function(e) {
if (timer) {
clearInterval(timer);
timer = null;
}
});