I need to create a function which can be executed only once, in each time after the first it won't be executed. I know from C++ and Java about static variables that can do the work but I would like to know if there is a more elegant way to do this?
var initialize = _. once(createApplication); initialize(); initialize(); // Application is only created once.
once function is used in conditions where we want a particular function to be executed only a single time. Even though we execute or call this function multiple times then also it will have no effect. The original function's values will only be returned each time it is called.
To count how many times a function has been called, declare a count variable outside of the function, setting it to 0 . Inside of the body of the function reassign the variable incrementing it by 1 . The count variable will store the number of function invocations.
If by "won't be executed" you mean "will do nothing when called more than once", you can create a closure:
var something = (function() { var executed = false; return function() { if (!executed) { executed = true; // do something } }; })(); something(); // "do something" happens something(); // nothing happens
In answer to a comment by @Vladloffe (now deleted): With a global variable, other code could reset the value of the "executed" flag (whatever name you pick for it). With a closure, other code has no way to do that, either accidentally or deliberately.
As other answers here point out, several libraries (such as Underscore and Ramda) have a little utility function (typically named once()
[*]) that accepts a function as an argument and returns another function that calls the supplied function exactly once, regardless of how many times the returned function is called. The returned function also caches the value first returned by the supplied function and returns that on subsequent calls.
However, if you aren't using such a third-party library, but still want a utility function (rather than the nonce solution I offered above), it's easy enough to implement. The nicest version I've seen is this one posted by David Walsh:
function once(fn, context) { var result; return function() { if (fn) { result = fn.apply(context || this, arguments); fn = null; } return result; }; }
I would be inclined to change fn = null;
to fn = context = null;
. There's no reason for the closure to maintain a reference to context
once fn
has been called.
Usage:
function something() { /* do something */ } var one_something = once(something); one_something(); // "do something" happens one_something(); // nothing happens
[*]Be aware, though, that other libraries, such as this Drupal extension to jQuery, may have a function named once()
that does something quite different.
Replace it with a reusable NOOP (no operation) function.
// this function does nothing function noop() {}; function foo() { foo = noop; // swap the functions // do your thing } function bar() { bar = noop; // swap the functions // do your thing }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With