Categories: Front-End JavaScript

JS-Callback : 等一會再來叫你

Home » JS-Callback : 等一會再來叫你

< 參考相關文章 >

請給予小肝肝工程師支持喔 'v' ETH/ERC20

JavaScript Callbacks :

“I will call back later!”

A callback is a function passed as an argument to another function

This technique allows a function to call another function

A callback function can run after another function has finished

以上為W3C 裡面所小結的敘述。

< 你還是說中文吧! >

W3C所說的….

A callback is a function passed as an argument to another function.

callback function 藉由當作函式參數帶入另一個功能陳述式.

中文翻譯字詞會用“回呼””、”回叫”、”回調”

我的話都習慣叫 “回扣” ( 疑?

回扣函式, 通常用於在目前的函式呼叫執行最後移交控制權而不使用函式回傳值的方式。

直接風格的控制權移交是不明確的,它是用回傳值的方式,然後進行到下一行程式碼或呼叫接下來其他函式。

前幾篇提到的同步的話要函式甲 > 函式乙 > 函式丙 > 函式丁, 你要等前面做完很麻煩 !

Callback function 的用途就在於可以讓我們的程式在不論同步或是非同步執行的狀況下都還是可以依照你要的順序執行函式

< 基本的參數帶入函式 >

我們這裡用直接風格帶入常數來解釋一下~

function fruitProcessor(apples, oranges) {
// 陳述式內的處裡方式  = 算式
 const juice = `Juice with ${apples} apples and ${oranges} oranges.`;
 return juice;
}

//代入參數
const appleJuice = fruitProcessor(5, 0);

console.log(appleJuice);

//代入參數
const appleOrangeJuice = fruitProcessor(2, 4);

console.log(appleOrangeJuice);


< 如何處理函式 >

我們範例的結構:

  1. 處理總函式
  2. 蘋果汁處理函式
  3. 柳橙汁處裡函式

let apples;
let oranges;

function fruitProcessor(callback)
{
    callback(apples,oranges);
}

function fruitAppleJuice (apples,oranges){
    apples= 5;
    oranges =0;
    const juice = `Juice with ${apples} apples and ${oranges} oranges.`;
    console.log(juice);
}

function fruitOrangeJuice (apples,oranges){
    apples= 2;
    oranges =4;
    const juice = `Juice with ${apples} apples and ${oranges} oranges.`;
    console.log(juice);
}

fruitProcessor( fruitAppleJuice );

fruitProcessor( fruitOrangeJuice );

將你想要處裡的不同參數輸入不同的函式, 爾後只要呼叫過來做處理即可。


< Callback簡單函式參數帶入+呼叫>

let juice;

function fruitProcessor(apples,oranges,callback)
{
    const juice = `Juice with ${apples} apples and ${oranges} oranges.`;
    callback( juice);
}

function fruitAppleJuice (callback)
{    
    console.log(juice);
 }

 fruitProcessor(5,1, fruitProcessor);


//執行結果
Juice with 5 apples and 1 oranges.

* 可以自己comment : callback( juice ), 試著自己想想看其中差異!

< Callback用多函式呼叫_控制執行順序 >


let apples;
let oranges;

function fruitProcessor(callback)
{
    callback(apples,oranges);
}

function fruitAppleJuice (Fn){

   apples= 5;
   oranges =0;
   const juice = `Juice with ${apples} apples and ${oranges} oranges.`;
   console.log(juice);
   Fn()
}

function fruitOrangeJuice (apples,oranges){
    
    apples= 2;
    oranges =4;
    const juice = `Juice with ${apples} apples and ${oranges} oranges.`;
    console.log(juice);
}

fruitProcessor(fruitAppleJuice(fruitOrangeJuice));

結論: fruitAppleJuice 去呼叫執行 fruitOrangeJuice

那你會問…. 既然是A函式去呼叫B函式, 這個有甚麼特別的呢??

有時候阿~ 我們會遇到延遲以及函式需要等待一段時間後執行, 主函式要是遇到錯誤不執行…. 那就是掛點在那了….

在本處我就不講解延遲執行這個方法了, 可以回去看一下我寫的 SetTimeout 的文章

< Callback Settimeout執行時間點 >

我們在主函式裡, 加入 SetTimeout 方式做控制執行.


let apples;
let oranges;

function fruitProcessor(callback)
{
    setTimeout(function(){ 
        console.log("這個是最先還是最後?");
         },0)

    callback(apples,oranges);
}

function fruitAppleJuice (Fn){
   apples= 5;
   oranges =0;
   const juice = `Juice with ${apples} apples and ${oranges} oranges.`;
   console.log(juice);
   Fn()
}

function fruitOrangeJuice (apples,oranges){
    apples= 2;
    oranges =4;
    const juice = `Juice with ${apples} apples and ${oranges} oranges.`;
    console.log(juice);
}

fruitProcessor(fruitAppleJuice (fruitOrangeJuice));


//執行結果

Juice with5apples and 0 oranges.
Juice with2apples and 4 oranges.
這個是最先還是最後?

Settimeout不管延遲時間設定多少,它都會最後一個執行

結論
這章節最主要是給你知道, callback的執行過程

lioajimzen

Share
Published by
lioajimzen

Recent Posts

Vue – 運行ES Module,import與export

import 與 export 在Node.js 有提到其用法: Node.js require、module以及exports 模組設定 - Hugo Habor 每一個Javascript檔案可當作獨立模組系統(ES Module) 1. Module(模組)…

12 months ago

當你的Windows應用程式完全無法執行時,該怎麼辦?

作者今天遇到一個很奇怪的問題 寫程式寫到一半... 點擊應用程式時... 怎麼都跳不出畫面... 後來查詢了一下是Reg註冊可能被微軟更新給搞掉了... 文章資訊來源: 【以解決】各位.exe檔案打不開,不要相信"在 Windows 7 或 Windows Vista 中無法打開 .EXE 檔案"這篇文章…

2 years ago

Python environments in VS Code-建立虛擬環境

Python environments in VS CodePython-建立虛擬環境

2 years ago

Node.js 核心模組-create-server

Node.js可以提供你很多模組,今天來探索如何使用 Node.js 的 http 模組來架設一個簡單的伺服器。 本篇使用模組require 載入你所需要的模組。這次我們使用'http' 模組來創造一個簡單的server。 資料參考 Node.js - createServer 起手式 - iT…

2 years ago

Node.js require、module以及exports 模組設定

針對Node.js來談談 require、module以及exports 模組設定。這些概念允許開發者將大型程序分解成小的、可管理的、可重用的部分,稱為模塊。 下面將逐一介紹這些概念,以及它們如何與 JavaScript 關聯。 An Essential Guide to Node.js Modules (javascripttutorial.net) 1. Module(模組)…

2 years ago

JSON與foreach的逐一條列應用。

像是很多專案必須讀取國家資料中心的metadata,許多檔案格式為CSV、JSON、Html可以利用網頁技術去爬蟲;拿一個範例來嘗試看看就知道這些語法的實際用途。 [主題週]專題報導-開放資料 (Open Data)相關議題與應用 (114230) - Cool3c 在這裡我們使用台北市資料大平台 UBike2.0 作為資料依據! 目的 來抓取空位數量 >15, 抓取空位數量 <15。…

2 years ago