Last active
May 2, 2023 11:25
-
-
Save noseratio/b14c135f9677caf61f0e5d144d78bce3 to your computer and use it in GitHub Desktop.
disposable event listener
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
if (!EventTarget.prototype.addDisposableEventListener) { | |
EventTarget.prototype.addDisposableEventListener = function(type, listener, options) { | |
this.addEventListener(type, listener, options); | |
return () => this.removeEventListener(type, listener); | |
} | |
} | |
const dispose = document.body.addDisposableEventListener( | |
'click', () => console.log('clicked!')); | |
try { | |
await new Promise(r => setTimeout(r, 5000)); | |
} | |
finally { | |
dispose(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment