Last active
August 29, 2015 14:06
-
-
Save eljojo/1fbd354988fee942ef5d to your computer and use it in GitHub Desktop.
example of what a callback is
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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