setTimeout(function () {
console.log("Timer");
}, 1000) // first argument is callback function and second is timer.
JS is a synchronous and single threaded language. But due to callbacks, we can do async things in JS.
setTimeout(function () {
console.log("timer");
}, 5000);
function x(y) {
console.log("x");
y();
}
x(function y() {
console.log("y");
});
// x y timer
// Another Example of callback
function printStr(str, cb) {
setTimeout(() => {
console.log(str);
cb();
}, Math.floor(Math.random() * 100) + 1)
}
function printAll() {
printStr("A", () => {
printStr("B", () => {
printStr("C", () => {})
})
})
}
printAll() // A B C // in order
// index.html
<button id="clickMe">Click Me!</button>
// in index.js
document.getElementById("clickMe").addEventListener("click", function xyz(){ //when event click occurs, this callback function (xyz) is called into callstack
console.log("Button clicked");
});
let count = 0;
document.getElementById("clickMe").addEventListener("click", function xyz(){
console.log("Button clicked", ++count);
});
function attachEventList() { //creating new function for closure
let count = 0;
document.getElementById("clickMe").addEventListener("click", function xyz(){
console.log("Button clicked", ++count); //now callback function forms closure with outer scope(count)
});
}
attachEventList();
Watch Live On Youtube below: