1 jQuery.fn.single_double_click = function(single_click_callback, double_click_callback, timeout) { 2 return this.each(function(){ 3 var clicks = 0, self = this; 4 jQuery(this).click(function(event){ 5 clicks++; 6 if (clicks == 1) { 7 setTimeout(function(){ 8 if(clicks == 1) { 9 single_click_callback.call(self, event); 10 } else { 11 double_click_callback.call(self, event); 12 } 13 clicks = 0; 14 }, timeout || 300); 15 } 16 }); 17 }); 18 }
以上核心应该就是那个setTimeout方法了,如果再指定的timeout或300毫秒之内没有第二次点击的话,就是单击事件,否则就是双击事件。
参考:https://gist.github.com/ncr/399624