Created
April 25, 2016 14:51
-
-
Save Gattermeier/f5b6682834d9756a324b00d70e7b840b to your computer and use it in GitHub Desktop.
memoize a function to only fire once.
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 once = function(func) { | |
var alreadyCalled = false; | |
return function() { | |
if (!alreadyCalled) { | |
func.apply(this, arguments); | |
alreadyCalled = true; | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To 'memoize' a function pass it as parameter into function 'once'.
For instance: