Using Both click and dblClick Events

Discussion in 'Javascript' started by Gene Wirchenko, Nov 18, 2011.

  1. Dear JavaScripters:

    Suppose that one wishes to have an action take place when an
    element is clicked and a different one when it is doubleclicked. How
    does the event processing work?

    From my checking under IE9, if I doubleclick, the click event
    will fire twice before the dblClick fires. How do I know when the
    second click will not be coming and can safely proceed with the click
    event action?

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Nov 18, 2011
    #1
    1. Advertising

  2. On Thu, 17 Nov 2011 20:10:56 -0800, Gene Wirchenko wrote:

    > From my checking under IE9, if I doubleclick, the click event
    > will fire twice before the dblClick fires. How do I know when the
    > second click will not be coming and can safely proceed with the click
    > event action?


    One approach that might work (untested):

    Create a counter for each element that might be either clicked or double
    clicked. Initialise the counter to 0.

    When the element is clicked, check the counter. If 0, increment the
    counter and call a delayed click event. 200 mSecs should be enough. If
    not 0, just increment the counter.

    In the delayed click event, if the counter is exactly 1, process it as a
    single-click.

    At the end of both click and double click event processing, set the
    counter back to 0.

    The code might look something like:

    <script type="text/javascript">

    var btn5ClickCount = 0;

    function btn5Clicked() {
    if (btn5ClickCount++ == 0) setTimeout("btn5DelayedSingleClick()",200);
    }

    function btn5DblClicked() {
    // do stuff
    btn5ClickCount = 0;
    }

    function btn5DelayedSingleClick() {
    if (btn5ClickCount == 1){
    // do stuff
    }
    btn5ClickCount = 0;
    }

    </script>

    <input type="button" id="btn5" name="btn5" value="Button 5"
    onclick="btn5Clicked()" ondblclick="btn5DblClicked()">

    Rgds

    Denis McMahon
     
    Denis McMahon, Nov 18, 2011
    #2
    1. Advertising

  3. Gene Wirchenko

    Tim Streater Guest

    In article <>,
    Gene Wirchenko <> wrote:

    > Dear JavaScripters:
    >
    > Suppose that one wishes to have an action take place when an
    > element is clicked and a different one when it is doubleclicked. How
    > does the event processing work?
    >
    > From my checking under IE9, if I doubleclick, the click event
    > will fire twice before the dblClick fires. How do I know when the
    > second click will not be coming and can safely proceed with the click
    > event action?


    As far as I am aware, you don't except by timing it yourself. On the
    first click set a timer in the single-click handler. If the second click
    occurs within xx msec (I use 250), then in the double-click handler you
    cancel the timer and proceed. If the timer goes off then the two clicks
    get treated separately. You also need a flag so that, on the second
    click within the period, the single-click handler does nothing.

    A nuisance if you ask me but unclear how to avoid it. Trouble is it
    means all single-click actions are delayed by 250msec.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Nov 18, 2011
    #3
  4. On Thu, 17 Nov 2011 at 20:10:56, in comp.lang.javascript, Gene Wirchenko
    wrote:
    >Dear JavaScripters:
    >
    > Suppose that one wishes to have an action take place when an
    >element is clicked and a different one when it is doubleclicked. How
    >does the event processing work?
    >
    > From my checking under IE9, if I doubleclick, the click event
    >will fire twice before the dblClick fires. How do I know when the
    >second click will not be coming and can safely proceed with the click
    >event action?


    In Windows the idea is that the first click selects something and the
    second click says do some action on the selected thing. Do you really
    want to make the first and second clicks so separate that you never want
    to do both ?

    The trouble with waiting long enough for a possible second click to
    happen is that the Control Panel allows the user to make Windows' double
    click timeout longer or shorter. If you make your timeout shorter than
    windows' worst case then it's a disaster waiting to happen. If, though,
    it's long enough then it will take a long time to recognise the first
    click. This will drive you mad when you're testing, never mind the
    customers.

    Something that would work is to do the first click action, then cancel
    or undo it if a double click is reported.

    John
    --
    John Harris
     
    John G Harris, Nov 18, 2011
    #4
  5. On Thu, 17 Nov 2011 20:10:56 -0800, Gene Wirchenko <>
    wrote:

    >Dear JavaScripters:
    >
    > Suppose that one wishes to have an action take place when an
    >element is clicked and a different one when it is doubleclicked. How
    >does the event processing work?
    >
    > From my checking under IE9, if I doubleclick, the click event
    >will fire twice before the dblClick fires. How do I know when the
    >second click will not be coming and can safely proceed with the click
    >event action?


    Thank you for the responses. I was wondering if I was missing
    something on this. I ran into the same issue when I was first
    starting out with Visual FoxPro years ago.

    I wonder how Windows deals with it internally.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Nov 18, 2011
    #5
  6. Gene Wirchenko

    Tim Streater Guest

    In article <>,
    Gene Wirchenko <> wrote:

    > On Thu, 17 Nov 2011 20:10:56 -0800, Gene Wirchenko <>
    > wrote:
    >
    > >Dear JavaScripters:
    > >
    > > Suppose that one wishes to have an action take place when an
    > >element is clicked and a different one when it is doubleclicked. How
    > >does the event processing work?
    > >
    > > From my checking under IE9, if I doubleclick, the click event
    > >will fire twice before the dblClick fires. How do I know when the
    > >second click will not be coming and can safely proceed with the click
    > >event action?

    >
    > Thank you for the responses. I was wondering if I was missing
    > something on this. I ran into the same issue when I was first
    > starting out with Visual FoxPro years ago.
    >
    > I wonder how Windows deals with it internally.


    Or Mac OS X or presumably Linux too. Same way, I expect. I know that
    under OS X if I double-click on something for which a single click is
    significant (usually select), it does the single-click action and then
    undoes it and does the double-click action.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Nov 18, 2011
    #6
  7. On Fri, 18 Nov 2011 20:24:25 +0000, Tim Streater
    <> wrote:

    >In article <>,
    > Gene Wirchenko <> wrote:
    >
    >> On Thu, 17 Nov 2011 20:10:56 -0800, Gene Wirchenko <>
    >> wrote:
    >>
    >> >Dear JavaScripters:
    >> >
    >> > Suppose that one wishes to have an action take place when an
    >> >element is clicked and a different one when it is doubleclicked. How
    >> >does the event processing work?
    >> >
    >> > From my checking under IE9, if I doubleclick, the click event
    >> >will fire twice before the dblClick fires. How do I know when the
    >> >second click will not be coming and can safely proceed with the click
    >> >event action?

    >>
    >> Thank you for the responses. I was wondering if I was missing
    >> something on this. I ran into the same issue when I was first
    >> starting out with Visual FoxPro years ago.
    >>
    >> I wonder how Windows deals with it internally.

    >
    >Or Mac OS X or presumably Linux too. Same way, I expect. I know that
    >under OS X if I double-click on something for which a single click is
    >significant (usually select), it does the single-click action and then
    >undoes it and does the double-click action.


    I am wondering about which events get revealed. If JavaScript
    (or Windows) held onto the click until the time for a doubleclick had
    expired before firing click, the problem with doubleclicking would not
    occur. Maybe.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Nov 18, 2011
    #7
  8. Gene Wirchenko

    Tim Streater Guest

    In article <>,
    Gene Wirchenko <> wrote:

    > On Fri, 18 Nov 2011 20:24:25 +0000, Tim Streater
    > <> wrote:
    >
    > >In article <>,
    > > Gene Wirchenko <> wrote:
    > >
    > >> On Thu, 17 Nov 2011 20:10:56 -0800, Gene Wirchenko <>
    > >> wrote:
    > >>
    > >> >Dear JavaScripters:
    > >> >
    > >> > Suppose that one wishes to have an action take place when an
    > >> >element is clicked and a different one when it is doubleclicked. How
    > >> >does the event processing work?
    > >> >
    > >> > From my checking under IE9, if I doubleclick, the click event
    > >> >will fire twice before the dblClick fires. How do I know when the
    > >> >second click will not be coming and can safely proceed with the click
    > >> >event action?
    > >>
    > >> Thank you for the responses. I was wondering if I was missing
    > >> something on this. I ran into the same issue when I was first
    > >> starting out with Visual FoxPro years ago.
    > >>
    > >> I wonder how Windows deals with it internally.

    > >
    > >Or Mac OS X or presumably Linux too. Same way, I expect. I know that
    > >under OS X if I double-click on something for which a single click is
    > >significant (usually select), it does the single-click action and then
    > >undoes it and does the double-click action.

    >
    > I am wondering about which events get revealed. If JavaScript
    > (or Windows) held onto the click until the time for a doubleclick had
    > expired before firing click, the problem with doubleclicking would not
    > occur. Maybe.


    But then there would have to be a noticeable delay before any
    single-click action was allowed to proceed. I can't remember the exact
    reasoning when developing this aspect for my app, but I know I wanted to
    clearly separate single and double-click actions. So in my case I had to
    accept that there would be a delay for single-click actions. But there
    are plenty of situations where that wouldn't apply so you don't want to
    penalise those too.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Nov 18, 2011
    #8
  9. On Fri, 18 Nov 2011 21:18:19 +0000, Tim Streater
    <> wrote:

    >In article <>,
    > Gene Wirchenko <> wrote:


    [snip]

    >> I am wondering about which events get revealed. If JavaScript
    >> (or Windows) held onto the click until the time for a doubleclick had
    >> expired before firing click, the problem with doubleclicking would not
    >> occur. Maybe.

    >
    >But then there would have to be a noticeable delay before any
    >single-click action was allowed to proceed. I can't remember the exact
    >reasoning when developing this aspect for my app, but I know I wanted to
    >clearly separate single and double-click actions. So in my case I had to
    >accept that there would be a delay for single-click actions. But there
    >are plenty of situations where that wouldn't apply so you don't want to
    >penalise those too.


    I know. I suspect, now having thought about it a bit more, that
    Windows does not ever have anything significant happen with both
    events. Clicking on something can give it focus. Big deal. This
    does not make a change.

    What we do not see -- correct me if you have an example -- of
    where both click and doubleclick do something significant. For
    example, click executing the object and doubleclick opening it in
    NotePad. If this is so, then it is a bit of smoke and mirrors.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Nov 18, 2011
    #9
  10. Gene Wirchenko

    Tim Streater Guest

    In article <>,
    Gene Wirchenko <> wrote:

    > On Fri, 18 Nov 2011 21:18:19 +0000, Tim Streater
    > <> wrote:
    >
    > >In article <>,
    > > Gene Wirchenko <> wrote:

    >
    > [snip]
    >
    > >> I am wondering about which events get revealed. If JavaScript
    > >> (or Windows) held onto the click until the time for a doubleclick had
    > >> expired before firing click, the problem with doubleclicking would not
    > >> occur. Maybe.

    > >
    > >But then there would have to be a noticeable delay before any
    > >single-click action was allowed to proceed. I can't remember the exact
    > >reasoning when developing this aspect for my app, but I know I wanted to
    > >clearly separate single and double-click actions. So in my case I had to
    > >accept that there would be a delay for single-click actions. But there
    > >are plenty of situations where that wouldn't apply so you don't want to
    > >penalise those too.

    >
    > I know. I suspect, now having thought about it a bit more, that
    > Windows does not ever have anything significant happen with both
    > events. Clicking on something can give it focus. Big deal. This
    > does not make a change.
    >
    > What we do not see -- correct me if you have an example -- of
    > where both click and doubleclick do something significant. For
    > example, click executing the object and doubleclick opening it in
    > NotePad. If this is so, then it is a bit of smoke and mirrors.


    I don't use Windows very much, but in the case OS X that may well be
    true. In the case of my own app It is definitely *not* the case.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Nov 18, 2011
    #10
  11. On Fri, 18 Nov 2011 at 14:13:21, in comp.lang.javascript, Gene Wirchenko
    wrote:

    <snip>
    > What we do not see -- correct me if you have an example -- of
    >where both click and doubleclick do something significant.

    <snip>

    In Windows File Manager, the first click says you want to use this
    picture, not that program that deletes all your files; the second click
    says Open it.

    Sounds pretty significant to me.

    John
    --
    John Harris
     
    John G Harris, Nov 22, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Fidge
    Replies:
    5
    Views:
    739
    S. Justin Gengo
    Jun 15, 2004
  2. Amy
    Replies:
    0
    Views:
    992
  3. ABCL
    Replies:
    0
    Views:
    552
  4. Terri I.
    Replies:
    0
    Views:
    111
    Terri I.
    Aug 13, 2003
  5. Tom de Neef

    How to ignore a dblclick

    Tom de Neef, Feb 27, 2008, in forum: Javascript
    Replies:
    2
    Views:
    121
    Tom de Neef
    Feb 28, 2008
Loading...

Share This Page