Javascript callback function example

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...");

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *