-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vladislav
committed
Jan 26, 2025
1 parent
16b0ec4
commit 8167ded
Showing
4 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
🛠 Базовый пример использования `AbortController` для отписки от слушателя событий | ||
|
||
По умолчанию в большинстве случаев для отписки стоит использовать `removeEventListener`. Но, если при подписке была использована анонимная функция, то отписаться от такого слушателя через `removeEventListener` не получится, так как мы вторым параметром должны передать ссылку на функцию-обработчик. | ||
|
||
```js | ||
const abortController = new AbortController() | ||
const element = document.querySelector('#element1') | ||
|
||
element.addEventListener('click', () => console.log('Подписка активна'), { | ||
signal: abortController.signal, | ||
}) | ||
|
||
// Вызываем когда захотим отписаться: | ||
controller.abort() | ||
``` | ||
|
||
🛠 Отписка сразу от нескольких обработчиков. | ||
|
||
`AbortController` может быть удобнее, чем `removeEventListener` в случае, если нам нужно отписаться сразу от нескольких обработчиков | ||
|
||
```js | ||
const abortController = new AbortController() | ||
const element1 = document.querySelector('#element1') | ||
const element2 = document.querySelector('#element2') | ||
|
||
element1.addEventListener('click',()=>{ | ||
// ... | ||
}, {signal: abortController.signal}) | ||
element2.addEventListener('click',()=>{ | ||
// ... | ||
}, {signal: abortController.signal}) | ||
|
||
// отписываемся одним вызовом сразу от двух обработчиков | ||
abortController.abort() | ||
``` | ||
|
||
🛠 Вешаем слушатель событий на `AbortController` | ||
|
||
В случае, если необходимо реализовать логику после отписки, то используя `AbortController` можно слушать событие `abort` на `AbortSignal` | ||
|
||
```js | ||
const abortController = new AbortController() | ||
const signal = abortController.signal | ||
|
||
signal.addEventListener('abort', () => { | ||
console.log('Операция была отменена'); | ||
}); | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
name: 'Окенчиц Владислав' | ||
url: https://t.me/Vlad_Okenchits | ||
photo: 'photo.jpg' | ||
--- | ||
|
||
Senior Frontend Developer |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.