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 01 Answer
Reset to default 8NEW 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
版权声明:本文标题:javascript - When to use AbortController to remove event listeners? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1741643973a2390078.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论