Skip to content

Instantly share code, notes, and snippets.

@MiguelCastillo
Created June 18, 2014 21:11
Show Gist options
  • Select an option

  • Save MiguelCastillo/3b80f06ce2318d296663 to your computer and use it in GitHub Desktop.

Select an option

Save MiguelCastillo/3b80f06ce2318d296663 to your computer and use it in GitHub Desktop.
steal js events
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