Last active
December 28, 2015 11:39
-
-
Save evitolins/7495355 to your computer and use it in GitHub Desktop.
A simple, single-use Javascript eventListener class. (http://jsbin.com/ADeZAHe/2/edit)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This class allows you to assign an eventListener to be utilzed only once, before | |
* it removes itself. | |
* | |
* # Usage Example: | |
* var elem = document.createElement('div'); | |
* var onetimer = new SingleEvent(elem, "click", function(){alert("Well, I'll never do THAT again...");}); | |
* document.body.appendChild(elem); | |
*/ | |
var SingleEvent = function (elem, eventName, callback) { "use strict"; | |
var singleuse; | |
if (typeof elem === "object" && elem.tagName) { | |
singleuse = function() { | |
callback.apply(); | |
elem.removeEventListener(eventName, singleuse, false); | |
}; | |
elem.addEventListener(eventName, singleuse, false); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SingleEvent=function(e,t,n){"use strict" | |
var a | |
"object"==typeof e&&e.tagName&&(a=function(){n.apply(),e.removeEventListener(t,a,!1)},e.addEventListener(t,a,!1))} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment