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..

1
  • 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. Commented Sep 9, 2011 at 15:36

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Do note that arguments.callee will throw an error in ES5 strict mode, and is not cross-browser.
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);}())

3 Comments

@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.
i agree but I wanted to know how to do it with an anonymous function
+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).
1

Try arguments.callee

console.log("ello"); setTimeout(arguments.callee,100);

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.