Web APIs

Listener once option

Try the free JavaScript compiler

Automatically unregisters the listener before its first invocation.

Formal Syntax & Structure

javascript syntaxjavascript
target.addEventListener(type, callback, { once: true })

Parameters & Attributes

NameTypeDescription
typestringEvent type.
callbackfunction | EventListener objectListener.

Working Example

Example Snippetjavascript
const b=document.createElement("button");let n=0;b.addEventListener("click",()=>n++,{once:true});b.click();b.click();console.log(n);
JavaScript Reference