Reputation: 351
How can I make my function not to repeat the multiple calls, if multiple calls were made in mins?
var s = 1;
function foo(x) {
if (s === 1) {
console.log('done');
setTimeout(function(){
s =1;
}, 10000);
} else {
s = 2;
console.log('no more repeat calling');
}
}
foo(1);
foo(2);
I am expecting the result -
done
no more repeat calling
Upvotes: 0
Views: 45
Reputation: 13346
Try this:
var s = 1;
function foo(x) {
if (s === 1) {
s = 0;
console.log('done');
setTimeout(function(){
s = 1;
}, 10000);
} else {
console.log('no more repeat calling');
}
}
foo(1); foo(2);foo(3);
Upvotes: 0
Reputation: 218818
Because s
is never being set to 2
. It looks like you meant to do that in the if block, rather than the else block:
if (s === 1) {
console.log('done');
s = 2; // <--- here
setTimeout(function(){
s = 1;
}, 10000);
} else {
console.log('no more repeat calling');
}
That way the first call will update the s
flag, so subsequent calls will go to the else
block.
Upvotes: 3