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) {
  alert(param);
}

// calls the test() function with callback() as argument
test( callback(8) );      // outputs 16
</script>
• 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"
  seconds++;
  document.getElementById('show_s').innerHTML = seconds;
}

// calls the addSeconds() to every second (1000 milliseconds)
setInterval('addSeconds()', 1000);
</script>
• 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
setTimeout(function(){
  alert('http://CoursesWeb.net/');
}, 2000);
</script>
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

  request.open("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) {
      callback(request.responseText);
    }
  }
}

// the callback function
function addResp(resp) {
  alert(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');
</script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag create a highlighted bolded text?
<q> <strong> <em>
<p>Address: <strong>http://CoursesWeb.net/</strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
}
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()
function someFunction() { alert("CoursesWeb.net"); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
Callback Functions in JavaScript

Last accessed pages

  1. Insert, Select and Update NULL value in MySQL (10632)
  2. PHP Simple HTML DOM Parser (4921)
  3. Add, Change, and Remove Attributes with jQuery (21328)
  4. Display data from PHP Array, or MySQL in HTML table (8710)
  5. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (43889)

Top accessed pages

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