Is it possible to make JQuery keydown respond faster?

前端 未结 3 1604
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 08:29

I am writing a simple page with JQuery and HTML5 canvas tags where I move a shape on the canvas by pressing \'w\' for up, \'s\' for down, \'a\' for left, and \'d\' for right

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 09:13

    It's not jQuery. You'll find the exact same delay in Notepad.

    What you'll have to do is this

    var keydown=false;
    $(document).on('keydown', function(e){
        if (!keydown) 
        {
            keydown=e.which;
            console.log('start moving and keep moving');
        }
    }).on('keyup', function(){
        console.log("stop moving");
        keydown=false;
    });​
    

提交回复
热议问题