繁体   English   中英

在javascript中长按按钮计算秒数

[英]Count seconds on button long press in javascript

我想使用javascript计算一次按下按钮(长按)的秒数,是否有任何在长按按钮时触发的功能或事件?

mousedown在鼠标mousedown触发, mouseup在释放时触发。

通过使用变量,您可以确定何时按下按钮,何时不按下。

您需要混合使用mousedownmouseup事件才能达到您想要的效果。

像这样的东西:

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM