本文介绍了节气门事件在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?

推荐答案

看看。

$('#search').keyup($.debounce(function() {
    // Will only execute 300ms after the last keypress.
}, 300));

这篇关于节气门事件在jQuery中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 14:14