本文介绍了jQuery如何处理click中的timer,dblclick分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the jQueryFileTree at http://abeautifulsite.net/notebook/58 and I want to distinguish between the dblclick and click events as a click is always triggered by a dblclick.

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

Googling about led to be a technique in which click is handled by a timer which kicks in when a dblclick does not cancel it.

有什么方法可以以优雅的方式与 jQuery 和 jQuery 示例一起使用吗?

Is there some way this can be used with jQuery and the jQuery example in an elegant manner?

推荐答案

可以使用setTimeout()clearTimeout()来实现定时器功能:

You can use setTimeout() and clearTimeout() to realize the timer functionality:

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');
    }
;

这篇关于jQuery如何处理click中的timer,dblclick分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 03:09