Created
September 3, 2013 19:03
-
-
Save EtienneLem/6428157 to your computer and use it in GitHub Desktop.
$('a').one('click', callback) is executed at most once *per element*.
$('a').one_for_all('click', callback) is executed at most once… *for all elements*.
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
// Works with Zepto/jQuery | |
$.fn.one_for_all = function(type, callback) { | |
var $all = $(this) | |
$all.on(type, function(e) { | |
$all.off(type) | |
callback(e) | |
}) | |
} |
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
$links = $('a') | |
// .one() | |
$links.one('click', function(e) { console.log('clicky') }) | |
$links.eq(0).trigger('click') // => clicky | |
$links.eq(0).trigger('click') | |
$links.eq(1).trigger('click') // => clicky | |
$links.eq(1).trigger('click') | |
$links.eq(2).trigger('click') // => clicky | |
$links.eq(2).trigger('click') | |
// .one_for_all() | |
$links.one_for_all('click', function(e) { console.log('clicky') }) | |
$links.eq(0).trigger('click') // => clicky | |
$links.eq(0).trigger('click') | |
$links.eq(1).trigger('click') | |
$links.eq(1).trigger('click') | |
$links.eq(2).trigger('click') | |
$links.eq(2).trigger('click') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment