Created
April 18, 2016 20:16
-
-
Save pgarciacamou/72bc18afe73ae6d1eeb9c99b864cdac1 to your computer and use it in GitHub Desktop.
Event Listener with destroyers.
This file contains 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
function addListener(elem, evtName, listener) { | |
elem.addEventListener(evtName, listener, false); | |
oneTimeListener(document, "motiondestroy", function () { | |
elem.removeEventListener(evtName, listener, false); | |
}); | |
} |
This file contains 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
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