force javascript EventListener to execute once?

Skizit picture Skizit · Feb 2, 2011 · Viewed 37k times · Source

I'm wondering is it possible for force a javascript event listener to, without the condition being true force it to execute once then continue listening for it's condition to be met to execute further?

Answer

Jlam picture Jlam · Dec 2, 2011
var clickFunction = function (event) {
    //do some stuff here
    window.removeEventListener('click',clickFunction, false );

};
window.addEventListener("click", clickFunction, false);

This will let you fire the clickFunction once, as the magic of closure let the removeEventListener find the clickFunction.

No need to use a library just to get something simple done.