How to call function only once? - javascript

How can I call the function only for once?
var myFunction = function () {
alert("calling function only for once");
}
myFunction();//alert "calling function only for once"
myFunction();//should not alert // if I call multiple times this should not be called

Try this:
var myFunction = function () {
alert("calling function only for once");
myFunction = function(){
return false;
}
}
myFunction();//alert "calling function only for once"
myFunction();//should not alert

Store some goobal variable a flag when run the function and check that variable at the start of the function.

set a flag, and call according to that flag:
var IsAlreadyCalled=false;
var myFunction = function () {
if(!IsAlreadyCalled){
alert("calling function only for once");
IsAlreadyCalled = true;
}
}
myFunction();//alert "calling function only for once"
myFunction();//should not alert

In your very odd scenario , the easiest way is to set a boolean:
var run = true,
myFunction = function(){
if(run){
alert('calling function only for once');
run = false;
} else {
return false;
}
};
myFunction(); // will run
myFunction(); // won't run
That way later on if you need to "reactivate" it you can just set the boolean back to true and call it again.
run = true;
myFunction(); // will run again

Other suggestions of using a flag are fine, but I would build it as a function decorator, that you can apply to any function. You avoid global variables this way, and your code becomes more readable and reusable:
// Takes a function and returns a function
// that executes only once
function once(f) {
var flag;
return function() {
if (!flag) {
flag = true;
return f.apply(this, arguments);
}
};
}
var fn = once(function() {
console.log('logged!');
});
fn(); // logged!
fn();
fn();
Demo: http://jsbin.com/povu/1/edit

Related

The argument should be executed as a function inside the body in javascript

I create a function as a variable to print 'Welcome' to console. I want to execute that variable as a function inside another new function. How can I do that?
This is my first function:
var first = function() {
console.log("Welcome");
}
You can just use it after it has been initialized:
var first = function() {
console.log("Welcome");
}
var second = function() {
first();
}
var firstFunction = function() {
//Your code that what you want to do
}
var second = function() {
firstFunction(); // calling first function
}

Most elegant way to stop a function executing on the first call?

I'm dealing with a stubborn API, and I need to stop a function executing on the first call, and then execute normally afterwards.
Currently I'm doing it like this:
var counter = 0;
function notFirstTime() {
counter++;
if (counter > 2) {
return;
}
}
Is there a better way of doing this?
You can use function that "returns function" for such cases:
function skipFirstCall(fun) {
var first_called = true;
return function() {
if (!first_called) {
fun.apply(this, arguments);
}
first_called = false;
}
}
var myFunc = skipFirstCall(function(){
console.log("I was executed!");
});
myFunc();
myFunc(); // I was executed!
myFunc(); // I was executed!
myFunc(); // I was executed!
You can pass any function to skipFirstCall function as argument to skip its first call:
var myAnotherFunc = skipFirstCall(function(){
console.log("myAnotherFunc was executed!");
});
myAnotherFunc();
myAnotherFunc(); // myAnotherFunc was executed!
myAnotherFunc(); // myAnotherFunc was executed!
Yes! there's a simple and elegant solution - you can redefine the function on the first call, like so:
var foo = function() {
foo = function() {
console.log("i'm after the 1st call");
}
}
The first time you call foo(), it rewrites the foo variable and sets it to a new function.

Disable a JavaScript function after it runs

I presume it is possible to create a JavaScript function that disables it self after it is done running.
Is possible? How can this effect be achieved?
Wrap arbitrary runnable in following manner:
function once(subject) {
var first = true;
return function() {
if (first) {
first = false;
return subject();
} else {
return null;
}
};
}
var wrapper = once(function() {alert("No more!");});
wrapper(); // alerts
wrapper(); // noop
Runnable will only be executed on first invocation of wrapper.
You can convert a function of arbitrary arguments to an argumentless runnable.
If you want the functionality to be happen only once you can use the following function
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
Courtesy: https://davidwalsh.name/essential-javascript-functions
something like this?
function a(){ alert(1); a = null;}
invoke a() once, second time it will say
Uncaught TypeError: a is not a function
if the function is anonymous, then make it IIFE
(function(){ alert(1);})();
var _flag = true; // Have a flag variable.
function oneTimer(){
// Check flag is set to true or not
if(!_flag) return;
_flag = false;
// Your function definition here.
}
As commented, if you want to execute a function only once, you should try IIFE. These functions are invoked immediately and cannot be called afterwards.
Following is a sample code.
(function test() {
console.log("test");
(function innerFunc() {
console.log("Inner Function");
})();
try {
innerFunc();
} catch (ex) {
console.log(ex)
}
})();
try {
test();
} catch (ex) {
console.log(ex)
}
Pretty easy, just assign an empty function to the function:
function once() {
alert('once');
once = function () { };
}
once();
once();

Return from calling function in Javascript

I have two function like below:
var doSomething = function() {
// first check if user wants to proceed and some other restrictions
checkIfReallyShouldProceed();
console.log('ok, proceed');
// proceed and do something
}
var checkIfReallyShouldProceed = function() {
var check = confirm('really proceed?');
if(!check){
//stop executing doSomething
}
}
doSomething();
If the user does not confirm I want to return from doSomething. Of course I could return the result of the check variable to doSomething and have something like
if(!checkIfReallyShouldProceed()){
return;
}
there, but I want the called function to stop the calling function from executing. Is this possible and if so, how?
An if condition is made for this type of conditional procedure:
var doSomething = function() {
if (checkIfReallyShouldProceed()){
return true; // This will stop the doSomething function from executing
}
console.log('ok, proceed');
}
var checkIfReallyShouldProceed = function() {
return confirm('really proceed?'); // returns true/false
}
doSomething();
In the checkIfReallyShouldProceed function, return whether or not the user wants to proceed. In doSomething, it will stop executing if the called method returns true

returning outer function from inner function

function MyFunction () {
if (SomeCondition) {
MyInnerFunction(SomeParam);
return;
}
if (SomeOtherCondition) {
MyInnerFunction(SomeOtherParam);
return;
}
if (SomeThirdCondition) {
MyInnerFunction(AnotherParam);
return;
}
function MyInnerFunction(Param) {
// Do some work here
// HERE: I want return from MyFunction
}
}
As you can see, when MyInnerFunction returns, the next statement to execute is the return statement of MyFunction. Is there a way to eliminate all these return statements so that the return from MyFunction executes inside MyInnerFunction?
Thanks.
you could reformat the code so that your if conditions are only modifying the parameters.
Then you just call your method with the correct parameters at the end
function MyFunction () {
var params;
if (SomeCondition)
params = SomeParam;
else if (SomeOtherCondition)
params = SomeOtherParam;
else if (SomeThirdCondition)
params = AnotherParam;
MyInnerFunction(params);
}

Categories

Resources