---
metadata:
  - name: generator
    content: Diplodoc Platform v5.54.4
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: zh/sdk/construct-3/throttle-debounce.md
    type: text/markdown
    title: Markdown version
  - href: ../../llms.txt
    type: text/markdown
    title: llms.txt
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/dev/games/doc/zh/llms.txt

# 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 %}

不建议每一刻使用 **Not more than once in \<X\> seconds**，这不会产生任何后果，但最好使用 **Every \<X\> seconds**。

![](../../_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/button-git.md -->
<a href="https://github.com/yandex-games-plugins/construct3">
  <span class="button">存储库</span>
</a>
<!-- endsource: zh/_includes/button-git.md -->
