# Throttle

## 定义

throttle和debounce非常像。唯一的区别就是，throttle会设置固定的时间间隔，即使事件还没有间断大于某个阈值，只要时间间隔到了，就会执行一次。

简单来说，throttle是在debounce最后会执行一次的基础上，穿插在中间固定时间间隔执行。

## ES6实现

```javascript
function throttle(fn, wait) {
    let timeout, lastTime = 0
    return (...args) =>{
        const currentTime = Date.now()
        if(current >= lastTime + wait){
            lastTime = current
            fn(...args)
        }else {
            clearTimeout(timeout)
            timeout = setTimout(()=> fn(...args), wait)
        }
    }
}
```


---

# 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/javascript/chang-jian-han-shu/throttle.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.
