我正在使用 jQueryFileTree http://abeautifulsite.net/notebook/58我想区分 dblclick 和 click 事件,因为点击始终由 dblclick 触发。
谷歌搜索发现了一种技术,其中点击由计时器处理,当 dblclick 没有取消它时计时器就会启动。
有什么方法可以以优雅的方式将其与 jQuery 和 jQuery 示例一起使用吗?
最佳答案
您可以使用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');
}
;
关于jquery - 如何使用jQuery来处理click、dblclick分离中的计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1472433/