function move() {
pos = pos+1;
t = setTimeout(move, 100);
}
Can that be called recursive? If yes, could you provide any reference?
Thanks in advance
function move() {
pos = pos+1;
t = setTimeout(move, 100);
}
Can that be called recursive? If yes, could you provide any reference?
Thanks in advance
No. The function is called by an external source (the timer), so it is not recursive.
So, here f1 ("move") is calling f2 ("setTimeout"), which in turn calls f1 again. Hmm.. this is a recursion if f2 is a callback function. But, if f2 is setting some property, like a "timeout". This isn't recursion.
No, the difference between recursion (func_a calls func_a) or indirect recursion (func_a calls func_b calls func_a) is that using a timer for repetitive calls will (decoupling) not grow the stack and previous state is lost.
It really depends on your definition of recursive, but I would say this is scheduling a callback to be called iteratively, not recursion.
Recursion involves breaking a problem down into smaller, similar problems, until you reach a base case, and then possibly combining the results from those into a solution. There is no "smaller" problem here; it's just scheduling the same callback to happen again after a certain time.
The problem with any sort of hard and fast definition is that recursion can be implemented in terms of iteration, and vice versa.
For instance, is this recursive?
function state1() {
doSomething();
return "state2";
}
function state2() {
doSomethingElse();
return "state1";
}
var state = "state1";
while (true) {
if (state == "state1") {
state = state1();
} else {
state = state2();
}
}
state1
and state1
each cause the other to be called; so in one sense, that's mutual recursion. It's driven by an iterative loop though, so it could also be considered iteration.
In a language that supports tail-call optimization, the same effect could be had by the two functions recursively calling each other (in fact, even in a language without tail-call optimization, you could do it, but you would run out of stack space very quickly):
function state1() {
doSomething();
state2();
}
function state2() {
doSomething();
state1();
}
So, the question really becomes, how do you distinguish whether something is recursive? Does the fact that one function causes itself to be called again make it recursive?
The code in question is not recursion - as the code is being called externally, not as part of a cyclic code-path back to the same method call.
Wikipedia has a great page about recursion here: Wikipedia: Recursion (computer science)
Yes it is .. but it can be called as Indirect recursion ..
example for Direct recursion would be something like this:
function factorial (n)
{
if(n==0)
{
return(1);
}
return (n * factorial (n-1) );
}
likewise others said .. the function calling itself ..
I would call it an inifite loop with a delay.
When the function returns, it does not get back into a previous call instance of itself, hence there is no deep "call stack" as it typically is in recursions.
No. Because you cannot "rewind" the local parameters as you return from called instances. Suppose your code was shaped like this:
function move() {
var l = pos;
pos = pos + 1;
if(pos == 100) return;
setTimeout("move", 100);
pos = l;
alert(pos);
}
You wouldn't be able to get a sequential hierarchy here. However if the code was like this,
function move() {
var l = pos;
pos = pos + 1;
if(pos == 100) return;
move();
pos = l;
alert(pos);
}
you could trace your steps back properly and implement logic based on that. Your code is "not-a-real-recursion" because it can be tail-optimized. Any code that can be tail-optimized is not recursive but iterative in nature. Tail recursion is just a different way of writing a loop.
technically it could be...
function move() {
var $this = this;
pos = pos+1;
t = setTimeout($this, 100);
}
but I see no reason why you'd wanna do this. and if you really wanna get silly (and lock up your browser's thread):
function move() {
var now = +new Date;
pos = pos+1;
while( ( +new Date - now ) <= 100 ){ }
move( );
}