Using Both ondblclick & onclick Handlers

Back from hell after long I post a workaround for the ondblclick and onclick javascript issue:
It’s not possible to define both handlers so that the onclick handler won’t get active on a doubleclick:

<input type="button"
            value="Example"
            onclick="MethodA();"
            ondblclick="MethodB();" />
 

A possible workaround is to use a semaphore-like global variable (here: dblclick). This is set to true whenever the doubleclick handler is performed.
Further we delay the single click handler for some miliseconds (here: 250) and check against the dblclick semaphore until continue.

var dblclick = false;
function MethodA()
{
     setTimeout(function()
     {
          if(dblclick == true)
               return;

          alert('MethodA');
     },250);
}
function MethodB()
{
     dblclick = true;
     alert('MethodB');
     setTimeout(function()
     {
          dblclick = false;
     },250);
}
 
This entry was posted in Web Development and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>