See this code.
The first function is executed while the button is pressed.
The second one is executed when the click is lifted. Notice that it calculates the time elapsed between the click and its survey, by a combination of onmousedown
and onmouseup
.
I think with this you can do what you want.
var btnClick = document.getElementById('btnClick');
var startTime, endTime;
/*Cuando se haga clic*/
btnClick.onmousedown = function() {
startTime = new Date();
console.log("Estoy presionado, haz lo que necesites...");
};
/*Cuando se deje de hacer clic*/
btnClick.onmouseup = function() {
endTime = new Date();
var timeDiff = endTime - startTime; //en ms
console.log("Se hizo clic:\n" + startTime);
console.log("Se levantó el clic:\n" + endTime);
console.log("Tiempo transcurrido:\n" + timeDiff + " ms");
};
<button id="btnClick">Haz Clic</button>