Skip to main content

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​

ParameterTypeDefaultDescription
typeSoundEventsEnumrequiredThe event type the listener was added for.
callback(event: SoundEvent) => voidrequiredThe same function reference that was registered.
filterSoundEventFilternoneThe 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, instanceId and instancePattern (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 addEventListener and once removes 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​