Skip to content

Instantly share code, notes, and snippets.

@pgarciacamou
Created April 18, 2016 20:16
Show Gist options
  • Save pgarciacamou/72bc18afe73ae6d1eeb9c99b864cdac1 to your computer and use it in GitHub Desktop.
Save pgarciacamou/72bc18afe73ae6d1eeb9c99b864cdac1 to your computer and use it in GitHub Desktop.
Event Listener with destroyers.
function addListener(elem, evtName, listener) {
elem.addEventListener(evtName, listener, false);
oneTimeListener(document, "motiondestroy", function () {
elem.removeEventListener(evtName, listener, false);
});
}
function oneTimeListener(elem, evtName, listener) {
elem.addEventListener(evtName, function callback() {
listener.apply(this, arguments);
elem.removeEventListener(evtName, callback, false);
}, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment