1

given the following code

(function(){console.log("ello");setTimeout("**CALL PARENT FUNCTION**",100);}())

how would one call this anonymous parent function every X duration..

samccone
  • 10,746
  • 7
  • 43
  • 50
  • The title should be changed to "Recursively calls to immediately invoked function expression". There is no such thing as an "anonymous function" in JavaScript. What you've written is a "function expression" - what you need is a "named function expression" so that it can be called from within its own function body. – Rick Sep 09 '11 at 15:36

4 Answers4

4

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.

Matt
  • 74,352
  • 26
  • 153
  • 180
Andrew D.
  • 8,130
  • 3
  • 21
  • 23
  • 1
    Do note that `arguments.callee` will throw an error in ES5 strict mode, and is not cross-browser. – Zirak Sep 09 '11 at 15:33
3

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);}())
Jeremy
  • 1
  • 85
  • 340
  • 366
  • 4
    @samccone: This function is practically anonymous; its name is only visible to itself. There are some ways to actually identify the current function but they're deprecated and it's clearer to do this. – Jeremy Sep 09 '11 at 15:31
  • i agree but I wanted to know how to do it with an anonymous function – samccone Sep 09 '11 at 15:35
  • +1 for beating me with almost the same answer I just typed (but then didn't bother submitting once I saw yours - stupid slow smartphone keyboard). – nnnnnn Sep 09 '11 at 15:44
1

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.

Community
  • 1
  • 1
Ryan Ternier
  • 8,714
  • 4
  • 46
  • 69
1

Try arguments.callee

console.log("ello"); setTimeout(arguments.callee,100);
Aaron Dougherty
  • 727
  • 6
  • 9