0

How can an inner function call a parent function after it has expired?

setTimeout(main, 2000);
function main(){
    /* .... code */
    setTimeout(console.log("hello after 5 seconds"), 5000);
}

The intended action is to print hello after 5 seconds in 5 seconds (7 total); with the above code it prints it in 2 seconds.

4
  • 1
    I assure you, it will happen :) Commented Aug 1, 2012 at 15:33
  • Is it you final code? You have sobre problems... Commented Aug 1, 2012 at 15:33
  • Your function1() code does not do anything, so how do you know it doesn't get called? Commented Aug 1, 2012 at 15:33
  • function1(){ return string; } is not valid! Commented Aug 1, 2012 at 15:35

3 Answers 3

3

You need to pass setTimeout function references. With setTimeout(console.log("hello after 5 seconds"), 5000);, you call console.log immediately. Any time you write () after a function name, you're invoking it.

console.log returns undefined, which is what is passed to setTimeout. It just ignores the undefined value and does nothing. (And it doesn't throw any errors.)

If you need to pass parameters to your callback function, there are a few different ways to go.

Anonymous function:

setTimeout(function() {
    console.log('...');
}, 5000);

Return a function:

function logger(msg) {
    return function() {
        console.log(msg);
    }
}

// now, whenever you need to do a setTimeout...
setTimeout(logger('...'), 5000);

This works because invoking logger simply returns a new anonymous function that closes over msg. The returned function is what is actually passed to setTimeout, and when the callback is fired, it has access to msg via the closure.

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

1 Comment

Answer completely reworked to address updated, clarified question.
2

I think I understood what you want. Take a look:

var main = function(){
    console.log("foo");
    var function1 = function( string ) {
        console.log("function1: " + string);
    };
    var function2 = function() {
        console.log( "hadouken!" );
    };
    // you will need to use a closure to call the function
    // that you want with parameters
    // if you dont have parameters, just pass the function itself
    setTimeout(function(){ function1("bar") }, 5000);
    setTimeout(function2, 6000);
}
setTimeout(main, 2000);

Or:

function main(){
    console.log("foo");
    function function1( string ) {
        console.log("function1: " + string);
    };
    function function2() {
        console.log( "hadouken!" );
    };
    // you will need to use a closure to call the function
    // that you want with parameters
    // if you dont have parameters, just pass the function itself
    setTimeout(function(){ function1("bar") }, 5000);
    setTimeout(function2, 6000);
}
setTimeout(main, 2000);

I usually prefer the first sintax.

jsFiddle: http://jsfiddle.net/davidbuzatto/65VsV/

Comments

1

It works! You miss word function.

setTimeout(main, 1000);

function main() {
    function function1 () { alert(1); };
    setTimeout(function1, 1000);
}​

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.