jQuery 中的限制事件调用 [英] Throttle event calls in jQuery
本文介绍了jQuery 中的限制事件调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 keyup
事件绑定到一个需要大约四分之一秒才能完成的函数.
I have a keyup
event bound to a function that takes about a quarter of a second to complete.
$("#search").keyup(function() {
//code that takes a little bit to complete
});
当用户输入一个完整的单词或快速按键时,该函数将被连续调用多次,并且需要一段时间才能完成.
When a user types an entire word, or otherwise presses keys rapidly, the function will be called several times in succession and it will take a while for them all to complete.
有没有办法限制事件调用,以便如果有几个快速连续调用,它只触发最近调用的那个?
Is there a way to throttle the event calls so that if there are several in rapid succession, it only triggers the one that was most recently called?
推荐答案
看看 jQuery Debounce.
$('#search').keyup($.debounce(function() {
// Will only execute 300ms after the last keypress.
}, 300));
这篇关于jQuery 中的限制事件调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文