Menu

gitpiper

listenOnce javascript Code Snippet in 2025

browsereventbeginner

Last Updated: 29 January 2025

Adds an event listener to an element that will only run the callback the first time the event is triggered.

  • Use EventTarget.addEventListener() to add an event listener to an element.
  • Use { once: true } as options to only run the given callback once.
const listenOnce = (el, evt, fn) => el.addEventListener(evt, fn, { once: true });
listenOnce( document.getElementById('my-id'), 'click', () => console.log('Hello world') ); // 'Hello world' will only be logged on the first click

javascript snippet similar to listenOnce For You in January 2025

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2025 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️