0

jQuery には、リクエストのスロットリングに使用できるツールはありますか? オートコンプリートの仕組みに似たもの。

より具体的には、これは私がやろうとしていることのようなものです:

    **顧客エントリ:**
    名: J
    姓: スミ

    **の検索結果:**
    Joe Shmoe -編集ボタン-
    ジョン・スミス -編集ボタン-
    Jane Smith -編集ボタン-
    ジョー・スミザーズ -編集ボタン-

ユーザーがいずれかのボックスに何かを入力すると、自分でリクエストを作成したいと思います。その後、jQuery はリクエストを送信するタイミングと送信するかどうかを決定し、レスポンスを処理するためのコードを提供します。

4

1 に答える 1

0

以下は、リクエストを調整するために私が書いたコードです。

初期化:

var global = {};
global.searchThrottle = Object.create(requestThrottle);

使用法:

this.searchThrottle.add(function(){
         //Do some search or whatever you want to throttle 
 });

ソースコード:

// runs only the most recent function added in the last 400 milliseconds, others are 
// discarded
var requestThrottle = {};

// Data
requestThrottle.delay = 400; // delay in miliseconds

requestThrottle.add = function(newRequest){
 this.nextRequest = newRequest;
 if( !this.pending ){
  this.pending = true;
  var that = this;
  setTimeout( function(){that.doRequest();}, this.delay);
 }
}

requestThrottle.doRequest = function(){
 var toDoRequest = this.nextRequest;
 this.nextRequest = null;
 this.pending = false;
 toDoRequest();
}

Object.create()ソースコード(「Javascript:TheGoodParts」から取得):

if( typeof Object.create !== 'function'){
 Object.create = function(o){
  var F = function(){};
  F.prototype = o;
  return new F();
 };
}
于 2010-10-20T17:33:09.110 に答える