Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```
- once
- ```
- There are times when you prefer a given functionality only happen once, similar to the way you'd use an onload event. This code provides you said functionality:
- ```
- function once(fn, context) {
- var result;
- return function() {
- if(fn) {
- result = fn.apply(context || this, arguments);
- fn = null;
- }
- return result;
- };
- }
- ```
- Usage
- ```
- var canOnlyFireOnce = once(function() {
- console.log('Fired!');
- });
- canOnlyFireOnce(); // "Fired!"
- canOnlyFireOnce(); // nada
- ```
- The once function ensures a given function can only be called once, thus prevent duplicate initialization!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement