Created
July 5, 2010 04:55
-
-
Save satoruk/464017 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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