Reputation: 967
I'm learning about callback functions, and I have this code where I pass the initial function in as a parameter, into the final function.
I can't seem to work out why this isn't woking? It's supposed to turn the body element red, and also log the variable 'body' to the console.
let body = document.querySelector('body');
function callbackFunction() {
body.style.background = "red";
}
function anotherFunction(callback) {
console.log(body);
}
anotherFunction(callbackFunction);
Upvotes: 0
Views: 101
Reputation: 34598
Well the passed callback is never executed.
function anotherFunction(callback) { //<-- callback received
console.log(body);
//<-- ...but never executed
}
Just add an invocation:
function anotherFunction(callback) {
console.log(body);
callback(); //<-- call the callback
}
Upvotes: 4