Created
June 19, 2012 21:16
-
-
Save wetmore/2956582 to your computer and use it in GitHub Desktop.
Pattern to execute a callback after a function is called a certain number of times
This file contains hidden or 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 counter(times, callback) { | |
var ct = 0; | |
return function() { | |
ct++; | |
if (ct === times) | |
callback(); | |
}; | |
}; | |
// register a counter function | |
var f = counter(3, function() { | |
alert('lol'); | |
}); | |
f(); // nothing happens | |
f(); // more nothing | |
f(); // alerts 'lol' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment