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 adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
}
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
alert(content);
Click on the "echo" correct instruction.
echo "CoursesWeb.net" echo "CoursesWeb.net"; echo ""CoursesWeb.net";
echo "Address URL: http://CoursesWeb.net";
Callback Functions in JavaScript

Last accessed pages

  1. Bubbles3 (8622)
  2. Butterfly Kyodai (6559)
  3. Lucky Balls (391)
  4. Get search data from URL address in JavaScript (1501)
  5. AJAX with POST and PHP (7442)

Top accessed pages

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