given the following code
(function(){console.log("ello");setTimeout("**CALL PARENT FUNCTION**",100);}())
how would one call this anonymous parent function every X duration..
given the following code
(function(){console.log("ello");setTimeout("**CALL PARENT FUNCTION**",100);}())
how would one call this anonymous parent function every X duration..
Use the callee
attribute on the arguments
object:
(function(){console.log("ello");setTimeout(arguments.callee,100);})()
It is however, recommended on the MDC site:
Note: You should avoid using arguments.callee() and just give every function (expression) a name.
It is possible to refer to the current function as arguments.callee
. However, this is prohibited in ECMAScript 5's Strict Mode and will be removed in future editions. If you were going to use it anyway, it would look like this:
(function(){console.log("ello");setTimeout(arguments.callee, 100);}())
Instead, you should just give the function a name. Because you're using the function as an expression, the name won't be visible to anyone else, you're just giving it a way to refer to itself. This is the recommendation made in Mozilla's JavaScript docs for arguments.callee
:
Note: You should avoid using
arguments.callee()
and just give every function (expression) a name.
This is simple and shorter than using arguments.callee
:
(function f(){console.log("ello");setTimeout(f, 100);}())
You would want to implement recursion.
You can look at:
http://www.devhands.com/2009/06/javascript-recursive-settimeout/
Specifically the section on:
function(){
var t_count = 0;
(function(delay, count) {
setTimeout(function() {
if (count && ++t_count > count) return;
// do your stuff here
setTimeout(arguments.callee, delay);
}, delay);
})(200, 10);
}
Update: It is true that arguments.calee was deprecated in javascript, the SO Thread here talks about it: Why was the arguments.callee.caller property deprecated in JavaScript? , and there is another thread here that talks about how to stop settimeout in recursive function which does have information that you want.
Try arguments.callee
console.log("ello"); setTimeout(arguments.callee,100);