javascript callback function example
Example 1: javascript callback
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.');
callback(name);
}
processUserInput(greeting);
Example 2: callback function js
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.');
callback(name);
}
processUserInput(greeting);
Example 3: what are callback functions
funct printANumber(int number, funct callbackFunction) {
printout("The number you provided is: " + number);
}
funct printFinishMessage() {
printout("I have finished printing numbers.");
}
funct event() {
printANumber(6, printFinishMessage);
}
Example 4: javascript callback
function newCallback(callback) {
callback('This can be any value you want to return')
}
function actionAferCallback (callbackData) {
console.log(callbackData)
}
function requestCallback() {
newCallback(actionAferCallback)
}
Example 5: javascript pass function as parameter
function goToWork(myCallBackFunction) {
myCallBackFunction();
}
function refreshPage() {
alert("I should be refreshing the page");
}
goToWork(refreshPage);
Example 6: what are callbacks
names = ['Anthony', 'Betty', 'Camie']
function addName(name, callback){
setTimeout(function(){
names.append(name)
callback()
}, 200)
}
function getNames(){
setTimeout(function(){
console.log(names)
}, 100)
}
addName('Daniel', getNames)