Need javascript code for button press and hold
When the button is pressed, call window.setTimeout
with your intended time and the function x
, and set the timer again at the end of x
but this time with a smaller interval.
Clear the timeout using window.clearTimeout
upon release of the mouse button.
function holdit(btn, action, start, speedup) {
var t;
var repeat = function () {
action();
t = setTimeout(repeat, start);
start = start / speedup;
}
btn.mousedown = function() {
repeat();
}
btn.mouseup = function () {
clearTimeout(t);
}
};
/* to use */
holdit(btn, function () { }, 1000, 2); /* x..1000ms..x..500ms..x..250ms..x */
Just put the below toggleOn in the OnMouseDown and toggleOff in the OnMouseUp of the button.
var tid = 0;
var speed = 100;
function toggleOn(){
if(tid==0){
tid=setInterval('ThingToDo()',speed);
}
}
function toggleOff(){
if(tid!=0){
clearInterval(tid);
tid=0;
}
}
function ThingToDo{
}