code_junkie

Need javascript code for button press and hold

Nov 10th, 2011
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. function holdit(btn, action, start, speedup) {
  2. var t;
  3.  
  4. var repeat = function () {
  5. action();
  6. t = setTimeout(repeat, start);
  7. start = start / speedup;
  8. }
  9.  
  10. btn.mousedown = function() {
  11. repeat();
  12. }
  13.  
  14. btn.mouseup = function () {
  15. clearTimeout(t);
  16. }
  17. };
  18.  
  19. /* to use */
  20. holdit(btn, function () { }, 1000, 2); /* x..1000ms..x..500ms..x..250ms..x */
  21.  
  22. var tid = 0;
  23. var speed = 100;
  24.  
  25. function toggleOn(){
  26. if(tid==0){
  27. tid=setInterval('ThingToDo()',speed);
  28. }
  29. }
  30. function toggleOff(){
  31. if(tid!=0){
  32. clearInterval(tid);
  33. tid=0;
  34. }
  35. }
  36. function ThingToDo{
  37.  
  38. }
  39.  
  40. var isClicked = false;
  41. var clickCounter = 100;
  42. function fnTrackClick(){
  43. if(isClicked){
  44. clickCounter--;
  45. setTimeout(clickCounter * 100, fnTrackClick);
  46. }
  47. }
  48.  
  49. <input type="button" value="blah" onmousedown="isClicked=true;" onmouseover="fnTrackClick();" onmouseup="isClicked = false;" />
Add Comment
Please, Sign In to add comment