У меня есть шаблон .jade
extends layoutIn
block content
input(type="text", id="captureInput", style="width:100%")
pre(id="resultado", style="overflow-y: scroll; height:150px")
и javascript:
var texto = document.getElementById('captureInput'),
resultado = document.getElementById('resultado'),
teclas = {}; //acá guardamos tiempo de inicio y fin de cada tecla
alert("1");
texto.addEventListener('keydown', function(keyboardEvent) {
alert("2");
const timestamp = performance.now(),
keyName = keyboardEvent.key;
if (!teclas[keyName]) { //Sólo si no se está manteniendo presionado
teclas[keyName] = {down: timestamp};
}
});
не покажи первый alert, но второго никогда.
ЗАМЕТЬ: Тот же код с jquery функционирует 100 % со следующим кодом jquery функционируют
$(function () {
var captureInput = $('#captureInput');
$(captureInput).focus();
$(captureInput).keydown(function(event){ //inicio de presion
var timeDown = (new Date()).getTime();
var keyName=event.keyCode;
if (!teclas[keyName]) { //Sólo si no se está manteniendo presionado
teclas[keyName] = {down: timeDown};
}
});
Для того, чтобы оно функционировало в любом браузере, нужно надеяться на то, что объект document готов, чтобы использовать. Твой код javascript нужен помещать это в событие похвалите:
window.addEventListener('load', function(){
var texto = document.getElementById('captureInput'),
resultado = document.getElementById('resultado'),
teclas = {}; //acá guardamos tiempo de inicio y fin de cada tecla
alert("1");
texto.addEventListener('keydown', function(keyboardEvent) {
alert("2");
const timestamp = performance.now(),
keyName = keyboardEvent.key;
if (!teclas[keyName]) { //Sólo si no se está manteniendo presionado
teclas[keyName] = {down: timestamp};
}
});
});
Использование
window.addEventListener('load', function(){
равняйся в jQuery в:
$(function () {
и что это делает, состоит в том, чтобы надеяться на то, что документ готов, чтобы быть использованным