I want to count seconds that how much time the button is pressed (long press) at a time using javascript, is there any function or event which fires at the time of long press of button?
You need to mix mousedown
and mouseup
events in order to achieve what you want.
Something like this:
var timer = 0,
timerInterval,
button = document.getElementById("button");
button.addEventListener("mousedown", function() {
timerInterval = setInterval(function(){
timer += 1;
document.getElementById("timer").innerText = timer;
}, 1000);
});
button.addEventListener("mouseup", function() {
clearInterval(timerInterval);
timer = 0;
});
<button id="button">Button</button>
<div id="timer">0</div>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments