Skip to content

Instantly share code, notes, and snippets.

@wetmore
Created June 19, 2012 21:16
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 wetmore/2956582 to your computer and use it in GitHub Desktop.
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
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