Last active
October 23, 2017 11:48
-
-
Save alizhdanov/e8d7b8c61d5abd8055c4e516a9425ef8 to your computer and use it in GitHub Desktop.
addEventListener once - cross browser
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
// Regular version - unfortunately supported only by very lates browsers (chrome 60+, ff 55+, edge 16+, safari 10.1+) | |
el.addEventListener('eventName', fn, {once: true}) | |
// decision - working everywhere | |
el.addEventListener('eventName', function cb(event) { | |
// function code here | |
event.currentTarget.removeEventListener(event.type, cb) | |
}) | |
// all credits to https://developers.google.com/web/updates/2016/10/addeventlistener-once |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment