underscore.js debounce メソッドが必要なようです
基本的には、次のように使用します: (簡潔にするために jQuery を使用)
$('#searchfield').keyup(_.debounce(getSuggestions, 250));
これによりgetSuggestions、イベントが 250 ミリ秒発生しなかった場合にのみ関数が呼び出されます。したがって、入力している場合は、少なくとも 4 分の 1 秒間停止するまで何も起こりません。
それがどのように機能するかを以下に貼り付けます。関数の周りに一連のロジックをラップし、新しい関数を返します。関数型プログラミングは楽しくないですか?
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
_.debounce = function(func, wait, immediate) {
var timeout, result;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) result = func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) result = func.apply(context, args);
return result;
};
};