如何使用jQuery来处理click,dblclick分离中的计时器

vfc*_*sts 6 jquery timer click double-click

我在http://abeautifulsite.net/notebook/58上使用jQueryFileTree,我想区分dblclick 和click事件,因为点击总是由dblclick触发.

谷歌搜索引发了一种技术,其中点击由计时器处理,当dblclick不取消它时启动.

有没有什么方法可以用jQuery和jQuery示例以优雅的方式使用?

Fer*_*yer 7

您可以使用setTimeout()clearTimeout()实现计时器功能:

var timeout;
var delay = 500;  // Delay in milliseconds

$("...")
    .click(function() {
        timeout = setTimeout(function() {
            // This inner function is called after the delay
            // to handle the 'click-only' event.
            alert('Click');
            timeout = null;
        }, delay)
    }
    .dblclick(function() {
        if (timeout) {
            // Clear the timeout since this is a double-click and we don't want
            // the 'click-only' code to run.
            clearTimeout(timeout);
            timeout = null;
        }
        // Double-click handling code goes here.
        alert('Double-click');
    }
;
Run Code Online (Sandbox Code Playgroud)