JSFiddle - React, Tailwind, and code Playground

by graphettion

HTML

<button class="button">Once</button>

CSS

body {
  background: #303030;
}

JavaScript

function _on(el, e, fn) {
  el.addEventListener(e, fn)
  return el
}

function _off(el, e, fn) {
  el.removeEventListener(e, fn)
  return el
}

function _once(el, e, fn) {
  fn = () => {
    _off(el, e, fn)
    console.log("Ran once")
  }
  _on(el, e, fn)
  return el
}

/* function _once(el, e, fn) {
  let result = () => {
    fn()
    _off(el, e, result)
  }
  _on(el, e, result)
} */

_once(document.querySelector(".button"), "click", () => {
	console.log("Ran once")
})