Created
June 18, 2014 21:11
-
-
Save MiguelCastillo/3b80f06ce2318d296663 to your computer and use it in GitHub Desktop.
steal js events
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
| for (var i in el[0]) { | |
| if (i[0] === 'o' && i[1] === 'n') { | |
| defineEvent(el, i.substr(2)); | |
| } | |
| } | |
| function defineEvent(el, evtType) { | |
| var evtDef = { | |
| name: evtType, | |
| count: 0 | |
| }; | |
| function fn() { | |
| console.log(this.name, this.count); | |
| this.count++; | |
| // 50 is a threashfold before we just unregister the event in case there is flooding | |
| if ( this.count > 50 ) { | |
| el.off(this.name, this.fn); | |
| } | |
| } | |
| evtDef.fn = fn.bind(evtDef); | |
| el.on(evtDef.name, evtDef.fn); | |
| return evtDef; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment