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.
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
}
See setInterval()
for more information on clearing timers.
I would use the javascript function setInterval()
within a function that gets called on mouse down.
<input type="button" id="button" onmousedown="inter=setInterval(startAction, 1*1000);"
onmouseup="clearInterval(inter);" value="click here" />
<script type="text/javascript">
function startAction(){
//whatever you want done
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With