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

标签 jquery

好的,所以我像在谷歌中一样进行搜索,您在输入中键入文本,它会立即为您提供条目。但我不喜欢这样。我使用类似的东西 $("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/

相关文章:

jquery - 响应式 CSS 边距

javascript - jQuery 验证 - 如何在类似表单的向导中使用步骤?

Javascript:firstChild对象返回未定义

javascript - jQuery 使用多选选项按颜色过滤数据

jquery - 速度测试.net api

javascript - 分离 javascript 函数的最佳方法是什么?

javascript - 当循环 div 时,jQuery 仅适用于第一个 div。我还在学习 PHP,我不知道哪里出了问题

javascript - 使用 Php 和 Ajax 发送部分电子邮件的联系表

jquery - 玩框架,jquery ajax调用冲突

javascript - jquery 在本地存储中保存背景颜色