Callback Functions are functions that are passed as arguments to other function.

- Example (see the comments in code):
<script type="text/javascript">
// defines a callback function
function callback(x) {
  return 2*x;

// defines a function that will use the callback() as argument
// param will represent the result returned by callback function passed as argument
function test(param) {

// calls the test() function with callback() as argument
test( callback(8) );      // outputs 16
• There are JavaScript functions that can be used only with callback function. For example: setTimeout() and setInterval().
Example, displays the number of seconds into a HTML element. Test yourself this example.
<div id="show_s"></div>
<script type="text/javascript">
var seconds = 0;
// used as callback function in setInterval()
function addSeconds(x) {
  // increments the seconds and adds the value in html tag with id="show_s"
  document.getElementById('show_s').innerHTML = seconds;

// calls the addSeconds() to every second (1000 milliseconds)
setInterval('addSeconds()', 1000);
• Anonymous functions can also be used as callback functions.
- Here is an exaple with setTimeout() and an anonymous function used as argument.
<script type="text/javascript">
// displays an alert window after 2 seconds
}, 2000);
The callback function can be very useful when we want to finish a long task, while allowing us to continue the execution of the JavaScript code.
- A good example is with an Ajax function that receives a callback function as argument. When the server response is completely received, it is passed to the callback function, but in the meen time the script continues to preccess the other instructions.
- If you want to test this example, you have to create a text file, named "test.txt", with some text, in the same folder in which you have the page with this script.
<script type="text/javascript">
// gets data from external file
// receives the URL address of the file, and a callback function that will be called when the response is received
function ajaxF(url, callback) {
  var request =  (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");      // sets the XMLHttpRequest instance"GET", url);			// define the request
  request.send(null);	   	// sends data

  // Check request status,  when the response is completely received pass it to callback function
  request.onreadystatechange = function() {
    if (request.readyState == 4) {

// the callback function
function addResp(resp) {

// calls the ajaxF() function, passing the file address, and the callback function
ajaxF('test.txt', addResp);

// continue the script while the Ajax and callback functions are executed
alert('Message 1');

Daily Test with Code Example

Which tag adds an image in web page?
<div> <img> <span>
<img src="" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
Callback Functions in JavaScript

Last accessed pages

  1. Create simple Website with PHP (12453)
  2. Dream Fruit Farm (273)
  3. Mahjongg Alchemy (3348)
  4. Zuma Deluxe (19375)
  5. Qwop (8220)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (49383)
  2. Read Excel file data in PHP - PhpExcelReader (37738)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (36753)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (33179)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (30548)