Ching Ching Ching Ching - 6 months ago 61
Javascript Question

Jquery ONE function PURE js

there! i have a problem when creating

function just like

here is the action :

one time execution event


<div id="justOnce">click me!</div>

function one(dom, event, callback) {
dom.addEventListener(event, function(e) { // add event
this.removeEventListener(event, callback); // remove it

one(document.getElementById("justOnce"), "click", function() {

alert("this alert only show once time");


what's wrong with my code?

thanks in advance...


Your code binds an event handler that removes callback as an event handler.

The only problem is … you never bound callback as an event handler in the first place.

You want something more like this:

function one(dom, event, callback) {
    function handler(e) {, e);
        this.removeEventListener(event, handler);
    dom.addEventListener(event, handler); 


  • You need to call the callback
  • You need to remove the event handler you actually bound