Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3: How do I set "click" event and "dbclick" event at the same time?

I've toggled click event to a node and I want to toggle a dbclick event to it as well. However it only triggers the click event when I dbclick on it.

So How do I set both events at the same time?

like image 644
SolessChong Avatar asked Aug 19 '13 05:08

SolessChong


2 Answers

I pretty much used the same logic as Jeremy D.

However, in my case, it was more neat to solve this thing with anonymous functions, and a little slower double click timeout:

dblclick_timer = false
.on("click", function(d) {
    // if double click timer is active, this click is the double click
    if ( dblclick_timer )
    {
        clearTimeout(dblclick_timer)
        dblclick_timer = false
        // double click code code comes here
        console.log("double click fired")
    }
    // otherwise, what to do after single click (double click has timed out)
    else dblclick_timer = setTimeout( function(){
        dblclick_timer = false
        // single click code code comes here
        console.log("single click fired")
    }, 250)
})
like image 85
Robin Manoli Avatar answered Oct 03 '22 18:10

Robin Manoli


You have to do your "own" doubleclick detection

Something like that could work:

var clickedOnce = false;
var timer;

$("#test").bind("click", function(){
    if (clickedOnce) {
        run_on_double_click();
    } else {
        timer = setTimeout(function() {
           run_on_simple_click(parameter);
        }, 150);
        clickedOnce = true;
    }
});

function run_on_simple_click(parameter) {
    alert(parameter);
    alert("simpleclick");
    clickedOnce = false;
}

function run_on_double_click() {
    clickedOnce = false;
    clearTimeout(timer);
    alert("doubleclick");
}

Here is a working JSFiddle

For more information about what delay you should use for your timer, have a look here : How to use both onclick and ondblclick on an element?

like image 35
Jeremy D Avatar answered Oct 03 '22 18:10

Jeremy D