Keyup not firing when keydown opens an alert

前端 未结 1 1077
花落未央
花落未央 2021-01-18 23:21

I have two event handlers, one for keydown and one for keyup. The keydown event handler triggers an alert message, but this prevents the keyup event from firing.

相关标签:
1条回答
  • 2021-01-19 00:10

    The alert prevents the event from happening. What you could do instead is trigger this function manually, because it happens anyways.

    var keyupfunction = function(e){
        alert('up');
        console.log('up');
    }
    
    window.addEventListener('keyup', keyupfunction);
    
    window.addEventListener('keydown', function(e) {
        if (i++ % 2) alert('down');
        console.log('down');
        keyupfunction(e);
    });
    

    But really, you shouldn't be using alerts. It prevents these events, but who knows what else it might break. Use something custom instead.

    0 讨论(0)
提交回复
热议问题