Javascript callback function example
Javascript callback function example
資料來源: Gemini
code
function sayHiAfterTime(name, callback) { setTimeout(function() { console.log("Hi, " + name + "!"); callback && callback(name); // Optional callback after greeting }, 2000); } function afterGreeting(name) { console.log("Finished greeting " + name); } sayHiAfterTime("Alice", afterGreeting); // Optional callback function console.log("Other code continues to run...");