gpt4 book ai didi

jquery - 如何限制 jQuery 事件的回调?

转载 作者:行者123 更新时间:2023-12-03 22:43:03 26 4
gpt4 key购买 nike

好的,所以我像在谷歌中一样进行搜索,您在输入中键入文本,它会立即为您提供条目。但我不喜欢这样。我使用类似的东西 $("TEXTINPUT").keyup(function() {。当用户输入速度非常快时,它会多次连接到数据库。我们是否有可能连接到 PHP仅当用户停止输入 1-2 秒时才文件,但不是立即?我需要在 jQuery 中执行此操作。谢谢。

最佳答案

如果您使用 Underscore Library就这么简单:

$("TEXTINPUT").keyup(_.throttle(function () {...}, 150));

来自 Underscore 网站的文档:

throttle   _.throttle(function, wait)

Returns a throttled version of the function, that, when invoked repeatedly, will only actually call the wrapped function at most once per every wait milliseconds. Useful for rate-limiting events that occur faster than you can keep up with.

还有debounce功能:

debounce   _.debounce(function, wait)

Calling a debounced function will postpone its execution until after wait milliseconds have elapsed since the last time the function was invoked. Useful for implementing behavior that should only happen after the input has stopped arriving. For example: rendering a preview of a Markdown comment, recalculating a layout after the window has stopped being resized...

关于jquery - 如何限制 jQuery 事件的回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8366043/

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