HTML defines onClick and onDblClick event handlers on all elements. However, if you want to enable both on the same element, something goes wrong. the onClick event gets fired twice! And that is not what we want, so here’s a little Javascript to fix that:

// use these functions to enable *both* onclick and 
// ondblclick on one element:
// usage: onclick="singleClick(someFunction)" 
//        ondblclick="doubleClick(otherFunction)"

singleClickTimeout = null;
function singleClick(fcn) {
    if (singleClickTimeout == null) {
        singleClickTimeout = setTimeout(function() {
            singleClickTimeout = null;
            fcn();
        }, 500);
    }
}
function doubleClick(fcn) {
    if (singleClickTimeout != null) {
        clearTimeout(singleClickTimeout);
        singleClickTimeout = null;
    }
    fcn();
}

0 Comments

Leave a Reply

Avatar placeholder

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.