I need your help. With the help of the following code, I am trying to detect the time that the Delete
key was pressed. My code doesn't give me any errors, but it doesn't work quite right. For example, if I press the key for 3 seconds, it shows a completely different time than it should. Tell me what I am doing wrong, what is my mistake? Thank you very much?)
var startTime;
document.body.onkeydown = function() {
const key = event.key;
if (key === 'Delete') {
console.log('key down');
startTime = +new Date();
}
}
document.body.onkeyup = function() {
const key = event.key;
if (key === 'Delete') {
console.log('key up')
var endTime = +new Date();
var time = (endTime - startTime) / 1000;
if (time > 3) {
console.log('cool')
}
console.log(time + ' sec');
}
}