# @ambassify/throttle 中文文档

规范地址：https://npmcn.com/package/%40ambassify/throttle/

版本：3\.0\.2
npm 元数据更新时间：2025-10-09T08:36:38.687Z
中文译文更新时间：2026-11-06T08:29:36.285Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40ambassify/throttle
原文对照：https://npmcn.com/package/%40ambassify/throttle/?lang=original
项目主页：https://github.com/ambassify/throttle#readme
源码仓库：https://github.com/ambassify/throttle

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：38.1 kB，16 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- Node.js 要求：\>=18
- 运行依赖：1 个
- 首次发布：2017-02-09T10:26:26.314Z
- 最新版本发布：2025-04-01T13:47:03.422Z
- 近 30 天下载量：3835（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：16（近 12 个月 0 个）

## 中文简介

取决于函数参数的喉咙 .

## 安装

```sh
npm install @ambassify/throttle
```

也可以使用：`pnpm add @ambassify/throttle`、`yarn add @ambassify/throttle`、`bun add @ambassify/throttle`

## README

# 喉咙

[![CircleCI](https://circleci.com/gh/ambassify/throttle.svg?style=svg&circle-token=8907f8b5ae62aab17b3ee1a1077bd1528c0cecb6)](https://circleci.com/gh/ambassify/throttle)

创建函数的节奏版本 .

## 安装

```shell
npm install --save @ambassify/throttle
```

## 使用量

创建节奏版本 `func`。 。 。 . `func` 将只在其 结果不是在节奏函数的缓存中,也不是在时间之间 当前和最后的引用大于 `delay`。 。 。 .

若为 `func` ,节奏函数将立即返回一个值 从缓存( 如果可用) 时 `func` 执行中.

**回返**数字 : `ThrottledFunction` - "func"的节奏版本

| 参数 | 类型 | 说明 |
| --- | --- | --- |
| 乐趣c | `function` | 节能 |
| 选项 | `ThrottleOptions` |  |

**示例**

```js
const throttle = require('@ambassify/throttle');

const throttled = throttle(<function-to-throttle>, <options>);

throttled('hello');
throttled.clear(<...args>);
```

### 旋律选项 : `Object`

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| 延迟 | `number` | 还要花多少时间 `func` 被再次引用 当有 被纠正的结果可用。 默认为 `Infinity`。 。 。 . |
| 最大年龄 | `number` | 允许在缓存中保存的物品有多长时间. 默认无限. |
| 最大大小 | `number` | 允许在缓存中保存的物品有多长时间. 默认无限. |
| 缓存 | `Map` | 指定用于节流的自定义缓存。 必须提供与地图等同的方法: 已经、 得到、 设定、 删除、 清除 |
| 解析器 | `function` | 同样的论据 `func` 仅返回缓存密钥构建中的重要内容 . |
| 已更新 | `function` | 与声讨 `cacheItem` 当项目更新时. |
| 错误 | `'clear'` | `'cached'` | `'persist'` | `function` | 当“ func” 丢出或拒绝时使用错误处理器 。 - `clear`: 缓存已清除并抛出错误 - `persist`: 错误被保存到缓存并抛出 - `cached`: 如果一个先前的值处于缓存中, 它会被返回, 如果错误不会被抛出的话—— 一个自定义函数, 它作为第一个参数接收错误, 而缓存项目作为第二个, 当指定的节奏函数不会在错误发生时触碰缓存时, 由这个处理器来与缓存项目交互 . |

### 弦乐功能 : `function`

节奏的功能.

**属性**

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| 清除 | `function` | 如果引用时没有任何参数,则整个缓存将被清除 **为:** 如果这些参数被传递,则这些参数的项目将被从缓存中删除. |

### 缓存项目

缓存项目通过 `onUpdated` 在节流阀选项中可以指定的回调.

-   **缓存项目。 初始化**: 项目是否具有初始值
-   **缓存项目。 待定**: 当节流阀正在运行但已经存在时, 尚未兑现的承诺 上一数值
-   **缓存项目. key**: 此缓存项的密钥
-   **缓存项目. stale**: 缓存项的值是否根据 上次更新和“延迟”选项.
-   **缓存项目.值**: 缓存项的当前值。 当这个设定,所有的计时器之类的 重设此项目
-   **缓存项目. error**: 与"值"相同,但用于表示被节制的结果是一个错误
-   **缓存项目. clear ()**: 将项目从节流器的缓存中清除
-   **缓存项目. 延迟( 延迟)**: 更新此特定项目的"延迟"
-   **缓存项目.max Age( 最大年龄)**: 更新此特定项目的"maxAge"

## 示例

```javascript
const throttle = require('@ambassify/throttle');

let example = 0;

async function myFunction(input) {
    // Delay 500 ms to fake a slow operation
    await new Promise(resolve => setTimeout(resolve, 500));

    example += input;
    return example;
}

// Allow `myFunction` to be called once every 2 seconds for each different
//`input` and cache the value for max 5 seconds.
const throttled = throttle(myFunction, { delay: 2000, maxAge: 5000 });

throttled(1); // 1
throttled(1); // 1
throttled(1); // 1

// Wait for 2 seconds
// "myFunction" is called, but the old cached result is returned immediately

throttled(1); // 1

// Wait 500ms (the fake delay)
// "myFunction" isn't called (as delay hasn't been reached) but we do get the
// latest result

throttled(1); // 2

// Wait for 5 seconds
// "myFunction" is called but the old cached value has expired so it resolves
// with the new value once the function has run

throttled(1); // 3

const conditional = throttle(myFunction, {
    delay: 2000,
    maxAge: 5000
    onCached: function(item) {
        // Only cache results for large values of input
        if (item.key < 10)
            item.clear();
    }
}

conditional(1); // 1
conditional(1); // 2
conditional(1); // 3

conditional(20); // 23
conditional(20); // 23
```

## 捐款

如果你有一些问题或代码,你想添加, 请随意打开一个拉请求或问题 我们会尽快调查.

## 许可证

我们要用麻省理工学院的许可放出这个.

## 关于我们

如果你想知道更多关于我们的事 请看看 [我们的网站](https://www.ambassify.com/),或者我们的Twitter账户 [@ 发音](https://twitter.com/Ambassify), (中文(简体) ), [场地基地](https://twitter.com/Sitebase), (中文(简体) ), [豪尔根 Evens](https://twitter.com/JorgenEvens)。 。 。 .

## 常见问题

### @ambassify/throttle 是什么？

取决于函数参数的喉咙 \.

### 如何安装 @ambassify/throttle？

在项目目录运行 npm install @ambassify/throttle，或使用 pnpm add / yarn add / bun add。

### @ambassify/throttle 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/ambassify\_\_throttle 是否存在。

### @ambassify/throttle 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @ambassify/throttle 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=18。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @ambassify/throttle 可以商用吗？

@ambassify/throttle 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @ambassify/throttle 的下载量有多少？

根据 npm 官方下载统计，@ambassify/throttle 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,835 次，日均约 128 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @ambassify/throttle 还在维护吗？

最新版本 3\.0\.2 发布于 2025/04/01（1\.6 年前）；至今共发布 16 个版本，其中过去 12 个月发布了 0 个；首次发布于 2017/02/09。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

