> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/chapter1/12javascript-quan-gong-lve-ff1a-ke-fu-js-de-qi-guai-bu-fen/123wu-jian-yu-han-shu/12320bi-bao-yu-hui-hu.md).

# 1.2.3.20.閉包與回呼

* 使用JavaScript的[一級函數](https://jenhsuan.gitbooks.io/javascript-node-js/content/chapter1/1235han-shu-jiu-shi-wu-jian.html)的特性, 將函數當成參數送入
  * 多虧有閉包, 三秒後可以取得greeting
* 回呼 (callback)
  * 我執行你, 當你結束後執行另一個函數

```
function sayHiLater() {
    var greeting = 'Hi';
    setTimeOut(function() {
        console.log('hi')
    }, 3000);
}

sayHiLater();

//jQuery uses function expressions and first-class functions!
$("button").click(function() {
});

function tellMeWhenDone(callback){
    var a = 1000;
    var b = 2000;

    callback();
}

tellMeWhenDone(function() {
    console.log('I am done');
});
```
