removeEventListener
Remove a listener that was added with addEventListener. Pass the same event type, the same callback and the same filter.
removeEventListener(type: SoundEventsEnum, callback: (event: SoundEvent) => void, filter?: SoundEventFilter): void;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
type | SoundEventsEnum | required | The event type the listener was added for. |
callback | (event: SoundEvent) => void | required | The same function reference that was registered. |
filter | SoundEventFilter | none | The filter the listener was added with. Leave it out only if the listener had none. |
Returns
Nothing.
Example
import { SoundHub, SoundEventsEnum, type SoundEvent } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');
const onStarted = (event: SoundEvent) => {
console.log('Started:', event.soundId);
};
soundHub.addEventListener(SoundEventsEnum.STARTED, onStarted, { soundId: 'music' });
// Same type, same callback, same filter
soundHub.removeEventListener(SoundEventsEnum.STARTED, onStarted, { soundId: 'music' });
Good to know
- Filters are compared field by field, not by reference, so a new object with the same values matches. All four fields count:
soundId,originalId,instanceIdandinstancePattern(compared by its source and flags). - A listener added with a filter is not removed by a call without one, and the other way round. If nothing matches, the call does nothing.
- The function returned by
addEventListenerandonceremoves exactly that listener, which is usually simpler than this method. - An inline arrow function cannot be removed this way, because you have no reference to pass back. Use the returned function for those.
See also
addEventListener: add a listener and get a remover back.removeEventListenersForInstance: drop every listener for one instance.hasEventListener: check whether any listener is left for a type.