Created
August 6, 2012 12:04
-
-
Save Lukewh/3273956 to your computer and use it in GitHub Desktop.
Execute a function once all dependencies have run.
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
| 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