Callback functions
Callback functions
*!*
function ask(question, yes, no) {
if (confirm(question)) yes()
else no();
}
*/!*
function showOk() {
alert( "You agreed." );
}
function showCancel() {
alert( "You canceled the execution." );
}
// usage: functions showOk, showCancel are passed as arguments to ask
ask("Do you agree?", showOk, showCancel);
The idea is that we pass a function and expect it to be "called back" later if necessary. In our case, showOk becomes the callback for the "yes" answer, and showCancel for the "no" answer.