Ching Ching Ching Ching - 1 year ago 146
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...

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download