...

View Full Version : On hold action



mabadi
11-04-2006, 10:30 PM
Hello

Can we someway create a custom on mouse hold (when you press and hold) function like the on click and on double click etc.. ??

brandonH
11-04-2006, 11:47 PM
I'm sure you can.

use the onmousedown handler to start a function that sets a varible to '1' and does a setTimeout(). on that same element have a onmouseup handler that sets that variable to '0'. so if the user doesnt hold the mouse down for the duration of the wait for the setTimeout, the variable gets set to '0' and the event you want to happen for the onHold wont happen. but if they hold the mosue down for the entire time of the setTimeout the variable stays set to '1'. and when the setTimeout runs it calls another function that checks that variable, if its '1' it does what you want, if not it does nothing (in my example i hae it alert you).




<input type=button value=hold onmousedown="OnHold();" onmouseup="clearHold();">

function OnHold(){
holdtrue='1';
duration='5';//change this to how many seconds you want the hold to last
setTimeout("checkHold()",duration*1000);
}
function checkHold(){
if(holdtrue=='1'){
alert('you held the mouse button down for 5 seconds');}
else{alert('you didnot hold the mouse down long enough to cause the hold trigger');}
}
function clearHold(){
holdtrue='0';
}


}

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum