JS Built-ins
EventTargetadvanced

EventTarget β€” once option fires listener only once

πŸ“– Usage Example
const et = new EventTarget()
et.addEventListener('signal', e => console.log(e.type))
et.dispatchEvent(new Event('signal'))  // β†’ 'signal'

The listener receives the Event object with type and other properties.

EventTarget.addEventListener β€” once option

Passing { once: true } as the third argument makes the listener auto-remove after its first invocation.

Challenge: Verify that a listener with { once: true } fires only once even when the event is dispatched twice.

πŸ’‘ Hints (1)
  • β€’ { once: true } is equivalent to adding a listener that calls removeEventListener on itself after the first call