鼠标按住时JavaScript重复操作 [英] JavaScript repeat action when mouse held down
本文介绍了鼠标按住时JavaScript重复操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一个JavaScript函数重复每隔这么多毫秒才能按下html按钮?如果可以使用标准JavaScript完成它会很棒,但使用jQuery或jQuery插件也会很棒。
Is there to have a JavaScript function repeated every so many milliseconds that an html button is held down? It would be great if this could be done with standard JavaScript, but using jQuery or a jQuery plugin would be great too.
推荐答案
在 mousedown()
事件中,代码启动重复计时器(在此示例中每隔500毫秒),一旦 取消,该计时器就会被取消mouseup()
事件发生。这应该适合你想要的:
On the mousedown()
event, this code starts a repeating timer (every 500ms in this example) that is cancelled as soon as the mouseup()
event occurs. This should be adaptable to what you want:
var intervalId;
$("#button").mousedown(function() {
intervalId = setInterval(do_something, 500);
}).mouseup(function() {
clearInterval(intervalId);
});
function do_something() {
// whatever
}
请参阅 setInterval()
了解有关清算计时器的更多信息。
See setInterval()
for more information on clearing timers.
这篇关于鼠标按住时JavaScript重复操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文