Skip to content

Function: onceEvent()

ts
function onceEvent<K>(
   el, 
   event, 
   handler): () => void;

Adds an event listener that is removed after the first call.

Type Parameters

Type Parameter
K extends keyof GlobalEventHandlersEventMap

Parameters

ParameterTypeDescription
elEventTargetThe target element.
eventKThe event name.
handler(event) => voidThe event handler.

Returns

A cleanup function.

() => void

Example

ts
const el = document.createElement('button')
document.body.appendChild(el)
onceEvent(el, 'click', () => console.log('clicked once'))
el.click()

Since

1.0.0

Released under the MIT License.