I have a script that differentiate single and double left clicks. Whereas dbl click is successfully detected and the assigned function is triggered upon event, single clicks are detected successfully but the assigned function is repeated infinitely upon event.
I have changed the original script for event delegation for I need dynamically generated DOM elements to respond with same functions to those events. After this change single click triggers the function assigned endlessly.
//single and double clicks
jQuery.fn.single_double_click = function(single_click_callback, double_click_callback, timeout) {
return this.each(function(){
var clicks = 0, self = this;
//delegated one:
jQuery('#subunitscontainer').on('click', jQuery(this), function(event){
//non delegated one:
//single click has no problem but dynamically generated dom
//does not response to the event here.
//jQuery(this).click(function(event){
clicks++;
if (clicks == 1) {
setTimeout(function(){
if(clicks == 1) {
single_click_callback.call(self, event);
} else {
double_click_callback.call(self, event);
}
clicks = 0;
}, timeout || 300);
}
});
});
}
//end single and double clicks