简体   繁体   English

Future.delayed 和 Timer 正在执行所有延迟的回调

[英]Future.delayed and Timer are executing all of the delayed callbacks

So this is the deal.所以这就是交易。 I am using Places autocomplete API in my app, so in Textfield's onChanged function I am making request to it for each letter typed.我在我的应用程序中使用 Places 自动完成 API,所以在 Textfield 的 onChanged function 中,我正在为每个输入的字母发出请求。 This costs a lot and I tried using Future.delayed to delay this call.这花费了很多,我尝试使用 Future.delayed 来延迟这个电话。 So I wrapped the whole function in Future.delayed and gave it a 2 seconds delay.所以我将整个 function 包裹在 Future.delayed 中,并延迟了 2 秒。 Now it waits 2 seconds but after 2 seconds it executes all the delayed call.现在它等待 2 秒,但 2 秒后它执行所有延迟的调用。

Example:例子:

typing: 'New York' (within 2 seconds) result: waits 2 seconds and executes following: Ne, New, New, New Y, New Yo, New Yor, New York..键入:'New York'(2 秒内)结果:等待 2 秒并执行以下操作:Ne、New、New、New Y、New Yo、New Yor、New York..

So basically it delays it but then executes it all at once.所以基本上它会延迟它,但然后立即执行它。 How do I solve this?我该如何解决这个问题?

PS: I did try to wrap onchanged function itself as well the function responsible for api calls. PS:我确实尝试过更改 function 本身以及负责 api 调用的 function 。

The onChanged function will trigger on every change, meaning for every letter that is typed. onChanged function 将在每次更改时触发,这意味着对于键入的每个字母。 Future.delayed does not change this behavior and instead delays every call of the function. Future.delayed不会改变这种行为,而是会延迟 function 的每次调用。

What you are looking for is debounce or throttle functions.您正在寻找的是去抖动或节流功能。 Consider using a library such as this that provides them.考虑使用像这样提供它们的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM