admin管理员组

文章数量:1296875

The new AbortController API in JavaScript allows us to remove multiple event listeners in one statement. So if we have 20 event listeners, we don't need to write 20 removeEventListener(); we can simply write controller.abort(); to remove them all.

Now, I'm wondering in what situations I'd be able to take advantage of this feature in a real application. I've never needed to remove a large number of event listeners at once.

(I already know that abort() can also be used to cancel a fetch request)

Thanks!

The new AbortController API in JavaScript allows us to remove multiple event listeners in one statement. So if we have 20 event listeners, we don't need to write 20 removeEventListener(); we can simply write controller.abort(); to remove them all.

Now, I'm wondering in what situations I'd be able to take advantage of this feature in a real application. I've never needed to remove a large number of event listeners at once.

(I already know that abort() can also be used to cancel a fetch request)

Thanks!

Share Improve this question asked Aug 28, 2021 at 18:07 Monster CatMonster Cat 811 silver badge4 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

NEW ANSWER: The answer given below was based on the Mozilla MDN Documentation. After some research, I found that there is in fact a way to remove an event listener with the AbortController Constructor. It's a new chrome88 feature release (Jan 2021) and may not have full adoption across all browsers yet, so the preferred method is still the old answer below.

The AbortController method was tested and seems to work in chrome & edge as of 09/21.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3>

本文标签: javascriptWhen to use AbortController to remove event listenersStack Overflow