---
metadata:
  - name: generator
    content: Diplodoc Platform v5.66.0
alternate:
  - https://yandex.com/dev/games/doc/en/sdk/construct-3/throttle-debounce.md
  - https://yandex.com/dev/games/doc/hi/sdk/construct-3/throttle-debounce.md
  - https://yandex.com/dev/games/doc/ko/sdk/construct-3/throttle-debounce.md
  - https://yandex.com/dev/games/doc/ru/sdk/construct-3/throttle-debounce.md
  - https://yandex.com/dev/games/doc/tr/sdk/construct-3/throttle-debounce.md
  - https://yandex.com/dev/games/doc/vi/sdk/construct-3/throttle-debounce.md
  - https://yandex.com/dev/games/doc/zh/sdk/construct-3/throttle-debounce.md
  - href: https://yandex.com/dev/games/doc/zh/sdk/construct-3/throttle-debounce.md
    type: text/markdown
    title: Markdown version
  - href: https://yandex.com/dev/games/doc/zh/llms.txt?revision=r21993288
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/dev/games/doc/zh/llms.txt?revision=r21993288

# Throttle 和 Debounce

<!-- source: zh/_includes/script-common.md -->
<!-- source: zh/_includes/script/index-js.md -->

<!-- endsource: zh/_includes/script/index-js.md -->

<!-- source: zh/_includes/script/requirements-js.md -->

<!-- endsource: zh/_includes/script/requirements-js.md -->

<!-- source: zh/_includes/script/image-modal-js.md -->

<!-- endsource: zh/_includes/script/image-modal-js.md -->
<!-- endsource: zh/_includes/script-common.md -->

插件提供了两种特殊条件的访问：**Not more than once in \<X\> seconds** 和 **On a deferred event**。

如果你更喜欢使用 JavaScript，可以参考以下文章：[以搜索框为例的 Debounce](https://doka.guide/js/debounce/){.external} 和 [以滚动页面为例的 Throttle](https://doka.guide/js/throttle/){.external}。

## Throttle {#throttle}

有时游戏的某些部分可能会严重影响性能。 例如，物理或人工智能的处理。

由于这类计算的复杂性，最好避免过于频繁地使用它们。 对于这种情况，通常使用 **Every \<X\> seconds** 来每隔一段时间运行一次代码，但这并不总是可行的。

例如，我们有一个复杂的游戏机制，它在点击屏幕时执行。 在这种情况下，当需要限制代码的执行频率，但无法使用定时器时，可以使用特殊的条件 **Not more than once in \<X\> seconds**。

{% note warning %}

不建议每个 tick 都使用 **Not more than once in \<X\> seconds**，这不会带来任何后果，但最好使用 **Every \<X\> seconds** 代替。

![Construct 3 截图：使用 YandexGamesSDK 插件配置排行榜的界面。一个三列（第三列的每个单元格中都有"+ 添加动作"和"+ 添加..."按钮）、三行的表格。第 1 行 —— "按钮"、"按下时"、"Yan... | 将 setScore 事件延迟 1 秒"。第 2 行 —— "YandexGames…"、"在延迟事件 "setScore" 时"、空单元格。第 3 行 —— "系统"设置图标、"authorized 为真"、"Yan... | 将分数 10 发送到排行榜 "score"，数据为 """。](../../_images/construct-3/leaderboards/debounce.webp)

{% endnote %}

## Debounce {#debounce}

Yandex Games SDK 的某些方法对特定时间内的请求数量有限制。 如果您忽视这些限制，您的游戏可能无法通过审核。

例如，如果您过于频繁地更新排行榜中的分数，那么您应该使用特殊的条件 **On a deferred event** 以及触发条件 **Delay execution**。 这个组合的工作原理类似于原生的 **Signal** → **On signal** 组合，但会将调用延迟到自上次尝试执行以来的特定时间。

然而，它可能会与之前的条件 **Not more than once in \<X\> seconds** 混淆，因为我们之前也用它来隐藏不应过于频繁调用的代码。

它们的主要区别在于，**Not more than once in \<X\> seconds** 限制调用频率，而 **On a deferred event** 将函数调用延迟到特定时间。 这正是我们需要的，因为我们不必过于频繁地执行函数，只需要不超过限制即可。

**Not more than once in \<X\> seconds** 和 **On a deferred event** 之间差异的可视化：

@[youtube](https://www.youtube.com/embed/gFpNtZ2lPM8?si=R3DgcZEtsShirW4n)

---


<!-- source: zh/_includes/sdk/button-git.md -->
<a href="https://github.com/yandex-games-plugins/construct3">
  <span class="button">存储库</span>
</a>
<!-- endsource: zh/_includes/sdk/button-git.md -->
