I'm trying to make a chrome extension to know order typing error, when the user misspells 11 instead of 1 sending an alert, but the script is running in a loop
document.querySelector('body').addEventListener('keydown', function() {
var tecla = event.keyCode;
if (tecla == 49) {
alert('Primeiro IF');
document.querySelector('body').addEventListener('keydown', function(event) {
var confirma = event.keyCode;
if (confirma == 49) {
alert('segundo IF');
return false;
}
});
} else if (tecla == 9) {
alert('Tecla 9');
return false;
}
});
You added a new event listener each time you typed
Try this:
let tecla, confirma;
document.querySelector('body').addEventListener('keydown', function() {
tecla = event.keyCode;
if (tecla == 49) {
if (confirma == 49) {
console.log('Segundo 1');
return false;
} else {
console.log('Primeiro 1');
confirma = tecla
}
} else if (tecla == 9) {
console.log('Tecla 9');
tecla = "";
confirma = "";
return false;
}
});