Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript function stringing

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");
}

like image 495
garyamorris Avatar asked May 23 '26 16:05

garyamorris


2 Answers

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

function mySecondFunction(func)
{
    alert("Second Function");
    func();
}
like image 109
DontVoteMeDown Avatar answered May 25 '26 06:05

DontVoteMeDown


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();
}
like image 26
Martin Majer Avatar answered May 25 '26 05:05

Martin Majer