Skip to content

Instantly share code, notes, and snippets.

@eljojo
Last active August 29, 2015 14:06
Show Gist options
  • Save eljojo/1fbd354988fee942ef5d to your computer and use it in GitHub Desktop.
Save eljojo/1fbd354988fee942ef5d to your computer and use it in GitHub Desktop.
example of what a callback is
function loadTweets() {
// first we start loading the tweets with ajax and set the callback to tweetsLoaded
$.get("/tweets", tweetsLoaded)
// then, we show a message stating that we're loading the tweets
$("#status").text("loading tweets")
}
function tweetsLoaded(tweets) {
// this function will add the tweets in the variable tweets to the screen
$("#status").text("tweets loaded")
}
// we execute the function loadTweets when the button is clicked. This is also a callback
$("button").click(loadTweets)
// a timeline of what will happen
// 1. user clicks button
// 2. ajax starts loading the tweets
// 3. status is shown as loading tweets
// 4. tweetsLoaded function is executed
// 4.1. tweetsLoaded function is supposed to add the tweets to the page
// 4.2. status is updated to tweets loaded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment