gpt4 book ai didi

javascript - AngularJS 去抖动未按预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 16:13:54 24 4
gpt4 key购买 nike

我需要回到我很久以前做的 AngularJS 项目(如果你已经深入了解 Angular 2,那将是多么痛苦的事情)。我需要通过在模型更改上使用去抖来替换搜索栏输入,因为我们的流量比当时多得多。每次击键时发送一个请求是可以的,但对于我们现在的访问者数量来说,这是 Not Acceptable 。

在 Angular 2 中,rxjs 的 debounce 可以解决我的问题,但在 AngularJS 中它无法按预期工作。虽然这个错误对我的工作流程并不重要,但我只是好奇这是否确实是预期的行为。

在 rxjs 中,去抖会做的是:

User interactions(keystrokes, each dash represents 100ms)
1--2---3----4-5-6-7----8-----9-----
1----2----3----5----7----8----9----
^ .debounceTime(500)

如您所见,操作 4 和 6 将被忽略,因为它们将被它们之后触发的操作所取代(在 500 毫秒的时间跨度内)。

但是在 AngularJS 中,使用 ng-model-options="{ debounce: 500 }",我得到:

User interactions(keystrokes, each dash represents 100ms)
1--2---3----4-5-6-7----8-----9-----
1----------------------8-----9-----
^ AngularJS

在此示例中,AngularJS 仅在 500 毫秒无任何内容时才会拾取更改。操作 1 - 8 之间的持续时间小于 500 毫秒,因此该持续时间内的更改永远不会更新。这是预期的行为吗?

最佳答案

每次用户更改输入时,防抖计时器都会重置。 From the Angular docs:

The updateOn and debounce properties allow you to specify a custom list of events that will trigger a model update and/or a debouncing delay so that the actual update only takes place when a timer expires; this timer will be reset after another change takes place.

关于javascript - AngularJS 去抖动未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41224009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com