How to apply long click event and doubleclick event on the same element in javascript

前端 未结 1 1254
无人及你
无人及你 2020-12-21 07:32

I have an element(textArea). Now I would like a long press event and a double click event on the element. I am able to do this but I would also like to use event.preventDefa

相关标签:
1条回答
  • 2020-12-21 08:37

    try this Demo

    HTML

    <input type="button" ondblclick="whateverFunc()" onmousedown="func(event)" onmouseup="revert()" value="hold for long"/>
    

    JavaScript

    var timer;
    var istrue = false;
    var delay = 3000; // how much long u have to hold click in MS
    function func(e)
    {
       istrue = true;
       timer = setTimeout(function(){ makeChange();},delay);
      // Incase if you want to prevent Default functionality on mouse down
      if (e.preventDefault) 
      { 
         e.preventDefault();
      } else {
         e.returnValue = false; 
      }
    }
    
    function makeChange()
    {
          if(timer)
          clearTimeout(timer);
    
          if(istrue)
          {
                /// rest of your code
              alert('holding');
    
          }
    }
    function revert()
    {
       istrue =false;
    }
    function whateverFunc()
    {
        alert('dblclick');
    }
    
    0 讨论(0)
提交回复
热议问题