synchronize two javascript function [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
i am trying to synchronize two javascript function which uses the same variable.like say for example i is a variable and its initial value is zero first function increase this value by 1 and other one decrease the value by 1 but after the first function call only
Thanks in advance.

You can set timeout between two function.. May be what u want is this...
<html>
<script>
var i=0;
window.setTimeout(function() {
window.setTimeout(function() {
}, 10);
}, 30);
function loop() {
var args = arguments;
if (args.length <= 0)
return;
(function chain(i) {
if (i >= args.length || typeof args[i] !== 'function')
return;
window.setTimeout(function() {
args[i]();
chain(i + 1);
}, 2000);
})(0);
}
function abc(){
//console.log("kishan");
i++;
console.log(i);
}
function def(){
//console.log("oza");
i--;
console.log(i);
}
for(var u =0 ;u<5;u++){
loop(abc,def);
}
</script>
</html>

Related

Can this If statement be written in one line? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Is there a one line way of doing a if statement that only fires once with a boolean?
var boolean;
if (!boolean) {
function doSomething();
boolean = true;
}
Something in the lines of this.
You could take the logical OR assignment ||= with a comma operator.
boolean ||= (doSomething(), true);
It does not make much sense to do it as one line since the code is clear the way you have it written (minus your syntax error), but it can be done
function test(myBool) {
function doSomething () { console.log('run'); }
myBool = myBool || doSomething() || true;
console.log(myBool);
}
test(false);
test(true);
or if doSomething returns a true boolean or truthy value
function test(myBool) {
function doSomething () { console.log('run'); return true; }
myBool = myBool || doSomething();
console.log(myBool);
}
test(false);
test(true);

Why return a function instead of just running that function again (recursion)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
If I want to run my function again inside that same function when a certain condition is met, what's the difference between returning that function VS just calling that function again.
function myFunc(param) {
if (param === "3") {
return myFunc(param);
}
}
VS
function myFunc(param) {
if (param === "3") {
myFunc(param);
}
}
If you need to return a result, then you need to return the result of the inner call recursively. For example:
function fact(n) {
if (n === 0) return 1;
return n * fact(n - 1);
}
console.log(fact(5));
But if the recursive function accomplishes what it needs to only inside the function - it's all side-effects from inside - then there's no need to return:
function addSpans(container, n) {
const span = container.appendChild(document.createElement('span'));
span.textContent = n;
if (n >= 1) addSpans(span, n - 1);
}
addSpans(document.body, 3);
return myFunc() does three things: (1) execute the function myFunc(), (2) when 1 finishes, take the result value and assign it as the return value of the current function, and (3) terminate the current function.
Calling myFunc() only does (1). That is the difference.

Javascript setInterval is not running [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
the following code returns nothing. Is there something I am missing here? Shouldn't this say 'hi' twice...
Thanks in advance
var done = 1;
var id;
id = setInterval(function() {
if(done > 3) {
console.log('hi');
done++;
} else {
clearInterval(id);
}
}, 500);
The if statement in the interval, directly terminated the interval because 1 > 3 == false
var done = 1;
var id;
id = setInterval(function() {
if(done < 3) {
console.log('hi');
done++;
} else {
clearInterval(id);
}
}, 500);

How to make a for loop that will go through functions without going through the same one twice [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have some functions, like:
function functionOne() {
/*
code
*/
}
function functionTwo() {
/*
code
*/
}
function functionThree() {
/*
code
*/
}
How can I write code to call each of the functions, once each (i.e. without repeats), in a random order?
Put your functions in an array, randomise the array and then iterate through it:
var functions = [
function () { alert("function 0"); },
function () { alert("function 1"); },
function () { alert("function 2"); },
function () { alert("function 3"); },
function () { alert("function 4"); }
];
functions.sort(function() { return 0.5 - Math.random() });
for (var i=0;i<5;i++) {
console.log(i);
functions[i]();
}
Here's a fiddle
this function should work:
function randCall(){
var array = [1,2,3];
while(array.length > 0){
var rand = (Math.random*array.length)|0; //converts to an Integer value
switch(array[rand]){
case 1:
functionOne();
break;
case 2:
functionTwo();
break;
case 3:
functionThree();
break;
}
array.splice(rand, 1);
}
}
hope this works and helps.

Add parameter to function dynamically [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
// set Process
i18n.setProcess(function() {diFunctions.getI18n('http://localhost/service/i18n/page?lang=eng&group=staff') });
// Setter and getter
this.setProcess = function( opProcess ) { //here param opProcess is function with parameters see i18n.setProcess() line of code
if( opProcess == undefined)
throw "Process is undefined";
if( $.isFunction(opProcess) == false )
throw "Process is not a function"
process = opProcess;
};
this.getProcess = function() {
return process;
};
See how i18n.setProcess passes a function with param as a parameter to setProcess.
Now i what i want in SetProcess is function() {diFunctions.getI18n('http://localhost/service/i18n/page?lang=eng&group=staff',**id**) // id is added dynamically to the function itself which was passed as parameter to setProcess
Problem - I want to add id dynamically(defined in my class variable always accesible by id) on set process with addition to functions parameter(Url,etc,etc,id). Functions parameters can grow but the id should be added last as a parameter?
Tried quite a few solutions but didnot work out? Check here
This is what the arguments object is for..
function foo() {
var lastArg = arguments.length ? arguments[arguments.length - 1] : 'default';
return lastArg;
}
foo(); // "default"
foo(1); // 1
foo(1, 2); // 2
If you want to write a function similar to bind which only sticks arguments on the end then you could do
function appendArguments(fn) {
var slice = Array.prototype.slice.call.bind(Array.prototype.slice),
args = slice(arguments, 1);
return function () {
return fn.apply(this, slice(arguments).concat(args));
};
}
now
var bar = appendArguments(foo, 3);
bar(); // 3
bar(4, 5); // 3 (because it's calling `foo(4, 5, 3)`

Categories