jQuery - Callback

A callback function is executed after the current effect/function is 100% finished. JavaScript statements are executed line by line. However, with effects, the next line of code can be run even though the effect is not finished/delayed. This can create errors. To prevent this, you can create a callback function.

A callback function is executed after a current effect (or some other function if supported) is finished. This is really more of a js concept and not so much as jQuery feature.

$({jquery selector}).{function name}(speed, {callback function});

The callback function is an anonymous function, not just another line of code!

Goodbye cruel world.

 


$("#btnHide").click(function ()
{
    //correct
    $("#demo1").hide(3000, function () { alert("Demo1 is now hidden"); });

    //NOT correct
    $("#demo1").hide(3000, alert("Demo1 is now hidden"));
});