Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Callback (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== JavaScript === In the following [[JavaScript]] code, function <code>calculate</code> uses parameter <code>operate</code> as a blocking callback. <code>calculate</code> is called with <code>multiply</code> and then with <code>sum</code> which act as callback functions. <syntaxhighlight lang="javascript"> function calculate(a, b, operate) { return operate(a, b); } function multiply(a, b) { return a * b; } function sum(a, b) { return a + b; } // outputs 20 alert(calculate(10, 2, multiply)); // outputs 12 alert(calculate(10, 2, sum)); </syntaxhighlight> The collection method {{code|.each()}} of the [[jQuery]] [[JavaScript libraries|library]] uses the function passed to it as a blocking callback. It calls the callback for each item of the collection. For example: <syntaxhighlight lang="javascript"> $("li").each(function(index) { console.log(index + ": " + $(this).text()); }); </syntaxhighlight> Deferred callbacks are commonly used for handling events from the user, the client and timers. Examples can be found in {{code|addEventListener}}, [[Ajax (programming)|Ajax]] and <code>[[XMLHttpRequest]]</code>. <ref>{{cite web |url=https://udn.realityripple.com/docs/Mozilla/Creating_JavaScript_callbacks_in_components#JavaScript_functions_as_callbacks |title=Creating JavaScript callbacks in components |department=Archive |website=UDN Web Docs |at=sec. JavaScript functions as callbacks |language=en |type=Documentation page |accessdate=2021-12-16 |url-status=live|archive-url=https://web.archive.org/web/20211216020616/https://udn.realityripple.com/docs/Mozilla/Creating_JavaScript_callbacks_in_components |archive-date=2021-12-16 }}</ref> In addition to using callbacks in JavaScript source code, C functions that take a function are supported via js-ctypes.<ref>{{cite web |url=https://developer.mozilla.org.cach3.com/en-US/docs/Mozilla/js-ctypes/Using_js-ctypes/Declaring_and_Using_Callbacks<!--This page is not properly rendered--> |title=Declaring and Using Callbacks |editor1-last=Holley |editor1-first=Bobby |editor2-last=Shepherd |editor2-first=Eric |department=Docs |website=[[Mozilla Developer Network]] |language=en |type=Documentation page |accessdate=2021-12-16 |url-status=live |archive-url=https://web.archive.org/web/20190117092921/https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Using_js-ctypes/Declaring_and_Using_Callbacks |archive-date=2019-01-17}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)