3

TIL — Removing DOM Event Handlers using AbortController

 2 years ago
source link: https://pawelgrzybek.com/til-removing-dom-event-handlers-using-abortcontroller/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

TIL — Removing DOM Event Handlers using AbortController

TIL — Removing DOM Event Handlers using AbortController

Published: 2022.06.18 | 1 minutes read

Thanks to Dan, who recommended “AbortController is your friend” by Sam Thorogood, today I learned about the ability to remove DOM event handlers using AbortController. Sam’s article is full of good use cases for this controller, but this one blew my mind.

const controller = new AbortController();

document.addEventListener(
  "mousemove",
  () => {
    // do some cool things here
  },
  { signal: controller.signal }
);

// later on
controller.abort();

Look at this example that tracks a mousemove event on the document, prints the coordinates and can be aborted on demand by clicking a button. So nice!

Leave a comment

Name:

Website (optional):

Twitter (optional):

GitHub (optional):

Comment:

👆 you can use Markdown here

Save my data for the next time I comment

© 2022 Copyright Pawel Grzybek. All rights reserved.
This site is built with Hugo and hosted on Netlify . The source code is hosted on GitHub.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK