Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Hour 4. Callbacks > What Is a Callback?

What Is a Callback?

If you are familiar with using jQuery, you are probably using callbacks on a regular basis. A callback is a function that is passed as an argument to another function and is generally called when the first function is finished. In the following jQuery example, a paragraph tag is hidden using jQuery’s hide() method. This method takes an optional argument of a callback function. If a callback function is given as an argument, it will be called when the hiding of the paragraph is complete. This makes it possible to do something when the hiding has finished—in this case, showing an alert.

$('p').hide('slow', function() {
  alert("The paragraph is now hidden");
});

A callback can be optional, however. If you do not want a callback, you could write this code:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial