const listenOnce = (el, evt, fn) => el.addEventListener(evt, fn, { once: true })
      
      #Example
      listenOnce(
        document.getElementById('elementId'),
        'click',
        () => console.log('Hello world')
      ); // 'Hello world' will only be logged on the first click

      Javascript language logo
      Listen for an event only once

      event
      listener

      The code above will create a listener that will only be called once.

      Shortcut: singleEventListener

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.