Skip to content

Instantly share code, notes, and snippets.

@Lukewh
Created August 6, 2012 12:04
Show Gist options
  • Select an option

  • Save Lukewh/3273956 to your computer and use it in GitHub Desktop.

Select an option

Save Lukewh/3273956 to your computer and use it in GitHub Desktop.
Execute a function once all dependencies have run.
var ReadyListener = function(itemLength, fn){
this.itemLength = itemLength;
this.haveItems = 0;
this.fn = fn;
};
ReadyListener.prototype.add = function(name){
console.log(' '+name+': Ready');
this.haveItems++;
this.haveAllCheck();
};
ReadyListener.prototype.haveAllCheck = function(){
if(this.haveItems === this.itemLength){
this.fn();
}
};
// Usage
var rl = new ReadyListener(lengthToAimFor, function(){
// To run once length has reached lengthToAimFor
});
// Do something with callback function
doSomething(function(){
rl.add('Done Something');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment