# 实现链式调用

## 题目

实现

```javascript
show(1000,()=>{console.log(1)})
    .show(1200,()=>{console.log(2)})
    .show(1300,()=>{console.log(3)})
```

完成等待1s，打印1，之后等待1.2s，打印2，之后打印1.3s，打印3

## 思路

1. 首先这个是链式调用，那么必然是返回一个变量，并且变量拥有show方法
2. 需要把延时任务统一串联起来，注意要用闭包共享状态，启动调用开始时间

## 实现

```javascript
const show = (time, fn) => {
	const task = []
  let isRun = false
  const run = ({time,fn}) =>{
  	isRun = true
  	setTimeout(()=>{
    	fn()
      task.shift()
  		isRun = false
      checkRun()
    }, time)
  }
  const checkRun = ()=> {
    if(task.length && !isRun){
    	run(task[0])
    }
  }
  const wrap = (time, fn) => {
  	task.push({time,fn})
    checkRun()
    return {show:wrap}
  }
  return wrap(time, fn)
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mm.ricky.moe/interview/inverview-record/tou-tiao-mian-jing/bi-shi/shi-xian-lian-shi-tiao-yong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
