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");
}
asked Dec 13, 2013 at 17:24
garyamorris
2,6073 gold badges18 silver badges23 bronze badges
2 Answers 2
Because you're doing nothing with that function in the parameter. You can do this:
function mySecondFunction(func)
{
alert("Second Function");
func();
}
answered Dec 13, 2013 at 17:25
DontVoteMeDown
21.5k10 gold badges72 silver badges113 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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();
}
answered Dec 13, 2013 at 17:27
Martin Majer
3,3724 gold badges26 silver badges37 bronze badges
1 Comment
garyamorris
Thanks for the help, you were spot on.
lang-js
$( document ).ready(function() { myFunction(); });in your case could be wrote$(myFunction)