[javascript] Function in JavaScript that can be called only once

JQuery allows to call the function only once using the method one():

_x000D_
_x000D_
let func = function() {_x000D_
  console.log('Calling just once!');_x000D_
}_x000D_
  _x000D_
let elem = $('#example');_x000D_
  _x000D_
elem.one('click', func);
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>_x000D_
<div>_x000D_
  <p>Function that can be called only once</p>_x000D_
  <button id="example" >JQuery one()</button>_x000D_
</div>
_x000D_
_x000D_
_x000D_

Implementation using JQuery method on():

_x000D_
_x000D_
let func = function(e) {_x000D_
  console.log('Calling just once!');_x000D_
  $(e.target).off(e.type, func)_x000D_
}_x000D_
  _x000D_
let elem = $('#example');_x000D_
  _x000D_
elem.on('click', func);
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>_x000D_
<div>_x000D_
  <p>Function that can be called only once</p>_x000D_
  <button id="example" >JQuery on()</button>_x000D_
</div>
_x000D_
_x000D_
_x000D_

Implementation using native JS:

_x000D_
_x000D_
let func = function(e) {_x000D_
  console.log('Calling just once!');_x000D_
  e.target.removeEventListener(e.type, func);_x000D_
}_x000D_
  _x000D_
let elem = document.getElementById('example');_x000D_
  _x000D_
elem.addEventListener('click', func);
_x000D_
<div>_x000D_
  <p>Functions that can be called only once</p>_x000D_
  <button id="example" >ECMAScript addEventListener</button>_x000D_
</div>
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to function

$http.get(...).success is not a function Function to calculate R2 (R-squared) in R How to Call a Function inside a Render in React/Jsx How does Python return multiple values from a function? Default optional parameter in Swift function How to have multiple conditions for one if statement in python Uncaught TypeError: .indexOf is not a function Proper use of const for defining functions in JavaScript Run php function on button click includes() not working in all browsers

Examples related to design-patterns

How to implement a simple scenario the OO way Implementing Singleton with an Enum (in Java) What is difference between MVC, MVP & MVVM design pattern in terms of coding c# Best Practices for mapping one object to another REST API Login Pattern When should we use Observer and Observable? How to implement a FSM - Finite State Machine in Java Function in JavaScript that can be called only once Thread Safe C# Singleton Pattern Repository Pattern Step by Step Explanation