Once a script has a function defined as a variable, it can pass that variable as parameter to another function. The following sample code demonstrates the technique: mainFunction(10, callbackDefinition); This code calls a named function, passing a number parameter and the name of the variable storing the callback function code. The outline of the function being called here must match these two parameters. The function receiving the callback variable will be able to call the function it stores.
Once a page has a function defined, including a callback, it can execute this code. Often, developers instruct the browser to listen for a user event, executing functions when this occurs. For example, the following HTML code calls a function:
- photo_camera Hemera Technologies/AbleStock.com/Getty Images