2

Could anyone explain to me why the third alert function is simply not called?, and a possible reading resource in relation to the error.

<script type="text/javascript">

$( document ).ready(function() {
   myFunction();
});

function myFunction()
{
    alert("First Function");

    mySecondFunction(function () {
        alert("Third Function");
    });
}

function mySecondFunction()
{
    alert("Second Function");
}

4
  • because you are not passing parameter to mySecondFunction Commented Dec 13, 2013 at 17:27
  • @SantoshJoshi he's passing a parameter, a function. He's just not executing it. Commented Dec 13, 2013 at 17:27
  • FYI $( document ).ready(function() { myFunction(); }); in your case could be wrote $(myFunction) Commented Dec 13, 2013 at 17:28
  • @DontVoteMeDown, my bad, i mean to say he has not declared function to take any paramaters, Commented Dec 13, 2013 at 17:30

2 Answers 2

8

Because you're doing nothing with that function in the parameter. You can do this:

function mySecondFunction(func)
{
    alert("Second Function");
    func();
}
Sign up to request clarification or add additional context in comments.

Comments

3

You are passing anonymous function function () { alert("Third Function"); } as a parameter to mySecondFunction(), but you're not calling this anonymous function anywhere inside mySecondFunction().

This would work:

function mySecondFunction(callback)
{
    alert("Second Function");
    callback();
}

1 Comment

Thanks for the help, you were spot on.

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.