Skip to content

Instantly share code, notes, and snippets.

@satoruk
Created July 5, 2010 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satoruk/464017 to your computer and use it in GitHub Desktop.
Save satoruk/464017 to your computer and use it in GitHub Desktop.
(function($, undefined) {
var lockKey = 'clickLockedId';
$.fn.releaseClickLocked = function(){
var tid = this.data(lockKey);
if(tid!==undefined){
clearTimeout(tid);
}
$(this).removeData(lockKey);
};
$.fn.singleClick = function(handler, timeout) {
timeout = timeout===undefined ? 3000:timeout;
$(this).click(function(evt){
var self = $(this);
if(self.data(lockKey)!==undefined){
return false;
}
var tid = setTimeout(function(){
self.releaseClickLocked();
},timeout);
self.data(lockKey, tid);
return handler.apply(this,[evt]);
});
};
})(jQuery);
// In 3 seconds, Called only once.
$('a.sample1').singleClick(function(evt){
alert('foo');
});
// In 1 second, Called only once.
$('a.sample2').singleClick(function(evt){
alert('foo');
}, 1000);
// The releaseClickLocked do to unlock immediately.
$('a.sample3').singleClick(function(evt){
var self = $(this);
setTimeout(function(){
self.releaseClickLocked();
},1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment