Skip to content

Instantly share code, notes, and snippets.

@asdf072
Forked from Victa/README
Last active December 14, 2015 05:09
Show Gist options
  • Save asdf072/5032954 to your computer and use it in GitHub Desktop.
Save asdf072/5032954 to your computer and use it in GitHub Desktop.
This fork of single_double_click is safe for .draggable()/.droppable()/.sortable(). It adds a second timer to keep track of drag time.
Code:
$(function(){
$("button").singleDoubleClick({
singleCallback: function(){ alert("You clicked once") },
doubleCallback: function(){ alert("You clicked twice") }
});
});
Markup
<button>Click Me!</button>
// Author: Jacek Becela
// Source: http://gist.github.com/399624
// License: MIT
(function(jQuery){
jQuery.fn.singleDoubleClick = function(options) {
return this.each(function(){
var defaults = {
singleCallback: function(){},
doubleCallback: function(){},
dragTimeout: 600,
dblClickTimeout: 300
};
var opts = jQuery.extend(defaults, options);
var clicks = 0, self = this, ms = 0, timer;
jQuery(this).mousedown(function(event){
ms = 0;
timer = setInterval(function(){ms += 50}, 50);
});
jQuery(this).mouseup(function(){clearInterval(timer)});
jQuery(this).click(function(event){
clicks++;
if (clicks == 1) {
setTimeout(function(){
if(ms > opts.dragTimeout){
// add a drag timeout callback if needed
}else if(clicks == 1){
opts.singleCallback.call(self, event);
} else {
opts.doubleCallback.call(self, event);
}
clicks = 0;
}, opts.dblClickTimeout);
}
});
});
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment