Creating One Time Executing Events in Javascript

javascript
Published on December 8, 2020

If we need a Javascript event to execute only once, we can use the once option as parameter to the addEventListener() method.

By setting the once option to true, the event listener will be invoked only once. After that it will be automatically removed.

document.querySelector("#my-button").addEventListener('click', function() {
	// event handler
}, {once: true});
In this Tutorial