[英]Count seconds on button long press in javascript
我想使用javascript计算一次按下按钮(长按)的秒数,是否有任何在长按按钮时触发的功能或事件?
mousedown
在鼠标mousedown
触发, mouseup
在释放时触发。
通过使用变量,您可以确定何时按下按钮,何时不按下。
您需要混合使用mousedown
和mouseup
事件才能达到您想要的效果。
像这样的东西:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.